使用MediaRecorder和FileOutputStream录制视频会生成无法播放的视频文件

时间:2013-01-29 09:33:34

标签: android file-descriptor mediarecorder fileoutputstream

我正在尝试实现可以​​多次启动和停止视频录制的功能,并将视频数据累积到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 path
  6. 上的android文件管理器
  7. 我可以看到创建的文件并“附加”了多个数据段。但它不会播放。它没有像其他视频文件一样的封面图片。
  8. 沿线某处,文件已损坏?如果我只记录一次并检查存储中的文件,它也不会起作用。如果我只为File中的MediaRecorder定义setOutputFile,我就可以录制视频,但我正在尝试为多次拍摄添加视频数据。这可能吗?

0 个答案:

没有答案