MediaRecorder停止Android失败

时间:2016-02-25 04:31:28

标签: java android android-mediarecorder

我使用此代码准备我的MediaRecorder录制视频。在此之后,我调用start()方法,该方法不会崩溃,但是当我调用stop()方法时,会发生崩溃并引发RuntimeException停止失败。我还注意到保存在设备中的视频文件已损坏,仅为32B。我假设我在以下方法中设置设备时出现错误。请注意,我正在尝试从屏幕上显示的surfaceView实时预览(如Snapchat)进行录制,而不是从本机相机应用中进行录制。

 private void initRecorder(Surface surface) throws IOException {
    // It is very important to unlock the camera before doing setCamera
    // or it will results in a black preview
    if(mCamera == null) {
        mCamera = Camera.open();
        mCamera.unlock();
    }

    if(mMediaRecorder == null)  mMediaRecorder = new MediaRecorder();
    mMediaRecorder.setPreviewDisplay(surface);
    mMediaRecorder.setCamera(mCamera);
    mMediaRecorder.setOnErrorListener(new MediaRecorder.OnErrorListener() {
        @Override
        public void onError(MediaRecorder mr, int what, int extra) {
            Toast.makeText(getApplicationContext(),
                    Integer.toString(what) + "_____" + Integer.toString(extra), Toast.LENGTH_LONG)
                    .show();            }
    });

    mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
    //       mMediaRecorder.setOutputFormat(8);
    mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
    mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
    mMediaRecorder.setVideoEncodingBitRate(512 * 1000);
    mMediaRecorder.setVideoFrameRate(30);
    mMediaRecorder.setVideoSize(640, 480);
    mMediaRecorder.setOutputFile(getVideoFile());

    try {
        mMediaRecorder.prepare();
    } catch (IllegalStateException e) {
        // This is thrown if the previous calls are not called with the
        // proper order
        e.printStackTrace();
    }

    mInitSuccesful = true;
}

0 个答案:

没有答案