Android MediaRecorder Multiple Files

回眸只為那壹抹淺笑 提交于 2019-12-04 13:19:42

问题


I am using a MediaRecorder to record video from the camera save it as .mp4 to the SDCard, which works flawlessly. I would like to limit the length of the .mp4 files to 10 seconds each and then automatically create a new file (ie: If a user records 30 seconds I would like to create three files, segment1.mp4 (0:00-0:10), segment2.mp4 (0:10-0:20), and segment3.mp4 (0:20-0:30)).

Here is where I set up the recorder and the output. What should I be doing to make this happen while recording?

private boolean prepareVideoRecorder() {

        this.mMediaRecorder = new MediaRecorder();
        this.mCamera.stopPreview();
        this.mCamera.unlock();
        this.mMediaRecorder.setCamera(mCamera);
        this.mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
        this.mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
        this.mMediaRecorder.setOrientationHint(90);
        this.mMediaRecorder.setMaxDuration(this.VIDEO_CHUNK_LENGTH_MS);  //10,000 ms
        if (getAndroidSDKVersion() >= 8) {
            this.mMediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));
        } else {
            this.mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
            this.mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
            this.mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
        }
        this.mVideoFileName = getOutputMediaFile(MEDIA_TYPE_VIDEO).toString(); // Returns a proper file name
        this.mMediaRecorder.setOutputFile(mVideoFileName);
        this.mMediaRecorder.setPreviewDisplay(mPreview.getHolder().getSurface());

        try {
            this.mMediaRecorder.prepare();
        } catch (IllegalStateException e) {
            Log.d(TAG, "IllegalStateException preparing MediaRecorder: " + e.getMessage());
            this.releaseMediaRecorder();
            return false;
        } catch (IOException e) {
            Log.d(TAG, "IOException preparing MediaRecorder: " + e.getMessage());
            this.releaseMediaRecorder();
            return false;
        }
        return true;
    }

来源:https://stackoverflow.com/questions/23343789/android-mediarecorder-multiple-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!