MediaRecorder停止Android失败

我使用此代码准备我的MediaRecorder来录制video。 在此之后,我调用start()方法,该方法不会崩溃,但是当我调用stop()方法时,会发生崩溃并且引发RuntimeException停止失败。 我还注意到保存在设备中的video文件已损坏,仅为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; }