设置MediaRecorder时为什么显示错误IllegalStateException?

时间:2014-04-05 07:16:23

标签: android android-camera

我的代码设置MediaRecorder,它在行集质量

时显示错误
mMediaRecorder = new MediaRecorder();

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

  mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
    mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
    mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
    mMediaRecorder.setProfile(CamcorderProfile .get(CamcorderProfile.QUALITY_HIGH));
    mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
    mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);

    // Step 4: Set output file
    mMediaRecorder.setOutputFile(getOutputMediaFile(MEDIA_TYPE_VIDEO).toString());

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

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

例如:

java.lang.IllegalStateException

堆栈跟踪:

java.lang.IllegalStateException
    at android.media.MediaRecorder.setOutputFormat(Native Method)
    at android.media.MediaRecorder.setProfile(MediaRecorder.java:366)
    at jp.osaka.E028.prepareVideoRecorder(E028.java:1441)
    at jp.osaka.E028.access$16(E028.java:1403)
    at jp.osaka.E028$6.onClick(E028.java:344)
    at android.view.View.performClick(View.java:3517)
    at android.view.View$PerformClick.run(View.java:14155)
    at android.os.Handler.handleCallback(Handler.java:605)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4503)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
    at dalvik.system.NativeStart.main(Native Method)

为什么在设置MediaRecorder时会显示错误IllegalStateException?

2 个答案:

答案 0 :(得分:16)

实际上你执行了mMediaRecorder.setOutputFormat()两次:明确地一次,之后mMediaRecorder.setProfile()尝试再次执行此操作,就像在堆栈跟踪中看到的一样。

Android Media Recorder对于类似的东西具有非常低的稳健性。

所以删除说

的行
mMediaRecorder.setOutputFormat();

并且错误应该消失。顺便说一下。除去

mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);

这也是mMediaRecorder.setProfile()已经完成的事情。

答案 1 :(得分:5)

您可能需要在MediaRecorder启动之前释放相机对象,例如:

private void releaseCamera() {
   if (myCamera != null) {
      // Release the camera object so other classes can use it.
      myCamera.release();
      myCamera = null;
   }
}

在启动MediaRecorder方法之前调用上述方法。

重要:此外,必须按以下顺序调用以下方法:

mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mMediaRecorder.setVideoSize(640,480);
mMediaRecorder.setVideoFrameRate(30);
mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);

这里的重要部分是最后调用setVideoEncodersetAudioEncoder