7

我正在尝试实现可以​​多次启动和停止视频录制并将视频数据累积到File.

这就是我准备媒体记录器的方式:

private boolean prepareVideoRecorder(){
    mMediaRecorder = new MediaRecorder();

    //0 for landscape
    //90 for portrait

    //Check for available profile
    CamcorderProfile profile = null;
    if(CamcorderProfile.hasProfile(CamcorderProfile.QUALITY_480P)){
        Log.d(TAG, "480p");
        profile = CamcorderProfile.get(CamcorderProfile.QUALITY_480P);
    }else if(CamcorderProfile.hasProfile(CamcorderProfile.QUALITY_720P)){
        Log.d(TAG, "720p");
        profile = CamcorderProfile.get(CamcorderProfile.QUALITY_720P);
    }else{
        Log.d(TAG, "LOW");
        profile = CamcorderProfile.get(CamcorderProfile.QUALITY_LOW);
    }

    // Step 1: Unlock and set camera to MediaRecorder
    mCamera.unlock();
    mMediaRecorder.setCamera(mCamera);

    // Step 2: Set sources
    mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
    mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);

    // Step 3: Set profile
    mMediaRecorder.setProfile(profile);

    // Step 4: Set output file and pass media recorder the file descriptor
    if(mStoreFile == null){
        mStoreFile = MediaUtil.getOutputMediaFile(MediaUtil.MEDIA_TYPE_VIDEO);
        try {
            mStoreFile.createNewFile();
        } catch (Exception e) {  
            e.printStackTrace();
        }
    }

    try {
        mOutputStream = new FileOutputStream(mStoreFile, true);
        mMediaRecorder.setOutputFile(mOutputStream.getFD());
    } catch (Exception e1) {
        e1.printStackTrace();
    }

    mMediaRecorder.setMaxDuration(30000);


    // Step 5: Set the preview output
    mMediaRecorder.setPreviewDisplay(mPreviewSurface.getHolder().getSurface());

    //Check orientation and set hint
    switch(mOrientation){
    case ORIENTATION_PORTRAIT_NORMAL:
        mMediaRecorder.setOrientationHint(90); 
        break;
    case ORIENTATION_PORTRAIT_INVERTED:
        mMediaRecorder.setOrientationHint(270); 
        break;
    case ORIENTATION_LANDSCAPE_NORMAL:
        mMediaRecorder.setOrientationHint(0); 
        break;
    case ORIENTATION_LANDSCAPE_INVERTED:
        mMediaRecorder.setOrientationHint(180); 
        break;
    }

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

这是点击代码上的按钮:

@Override
        public void onClick(View v) {
            if (isRecording) {
                try{
                    mOutputStream.close();
                }catch(Exception ee){
                }

                // stop recording and release camera
                mMediaRecorder.stop();  // stop the recording

                mMediaRecorder.reset();
                mCamera.lock();         // take camera access back from MediaRecorder

                // inform the user that recording has stopped
                mRecordButton.setImageResource(R.drawable.record_button);
                isRecording = false;

            } else {
                // initialize video camera
                if (prepareVideoRecorder()) {
                    // Camera is available and unlocked, MediaRecorder is prepared,
                    // now you can start recording
                    mMediaRecorder.start();

                    // inform the user that recording has started
                    mRecordButton.setImageResource(R.drawable.record_button_on);
                    isRecording = true;
                } else {
                    // prepare didn't work, release the camera
                    releaseMediaRecorder();
                    // inform user
                }
            }
        }
    });

MediaUtil.getOutputMediaFile(MediaUtil.MEDIA_TYPE_VIDEO)会给我类似的东西:/storage/sdcard0/Pictures/Project/VID_2013.mp4

当前问题:

我目前测试它的方式是:

  1. 开始录制
  2. 停止录制
  3. 开始录制
  4. 停止录制
  5. 转到 /Pictures/Project 路径上的 android 文件管理器
  6. 我可以看到创建并“附加”了多个数据段的文件。但它不会播放。而且它没有像其他视频文件那样的封面图像。

在某处,文件已损坏?如果我只记录一次并检查存储中的文件,它就不行了。如果我只FileMediaRecorderin定义一个,我可以录制视频setOutputFile,但我正在尝试为多次拍摄附加视频数据。这可能吗?

4

0 回答 0