Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

My code setting MediaRecorder, it show error at row set Quality

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

Stacktrace:

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)

Why show error IllegalStateException when setting MediaRecorder?

You need to change e.getMessage() to e.fillInStackTrace(). Then, post your stacktrace here! – ChuongPham Apr 5, 2014 at 7:41

Actually you do mMediaRecorder.setOutputFormat() twice: One time explicitly and afterwards mMediaRecorder.setProfile() tries to do it again as you can see in your stacktrace.

The Android Media Recorder has a very low robustness for things like that.

So remove the line that says

mMediaRecorder.setOutputFormat();

and the error should go away. And btw. remove

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

which is what mMediaRecorder.setProfile() has already done as well.

Probably yes. The video frame rate is part of the camcorder profile so you don't need to set it, but from my past experience the frame rate is used by most cameras only as a maximum value and the actal frame rate is set by the camera itself based on the actual conditions, for example the available light. So this parameter is quite uncritical for the success of the recorder start. – Nantoka Apr 16, 2014 at 8:32 You're a genius mate. Puzzling about even after reading all the official documentation by Google. Yeap, recorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH)) does already set the AudioEncoder and VideoEncoder for you already. – Morgan Koh Mar 22, 2018 at 7:49

You may need to release the camera object before the MediaRecorder start, with something like:

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

Call the above method before you start your MediaRecorder methods.

IMPORTANT: Also, the methods below MUST be called in this order:

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);

The important bits here are that setVideoEncoder and setAudioEncoder are called last.

No, I mean doing the above before mMediaRecorder.start();. I can't see from your post that you have this method declared. But it won't work without it. – ChuongPham Apr 5, 2014 at 8:26 If i close 3 row: setOutputFormat,setAudioEncoder,setVideoEncoder , it will work, But I want setting for them. – mum Apr 5, 2014 at 8:29 Ah, I see. You may need to move the mMediaRecorder's methods inside your try/catch and see what errors are returned (if any). The three methods you mentioned will depend on the Android API and whether your device physically support the modes declared within those methods. You may end up including another third-party library to do what you want e.g. ffmpeg. – ChuongPham Apr 5, 2014 at 8:36

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.