Why show an IllegalStateException error when setting up MediaRecorder?

My MediaRecorder code setting, it shows an error in a rowset 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;
   }

Example:

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 an IllegalStateException error when setting up MediaRecorder?

+4
source share
2 answers

In fact, you execute mMediaRecorder.setOutputFormat()twice: one time explicitly, and then mMediaRecorder.setProfile()tries to do it again, as you can see in your stack.

Android Media Recorder has very low reliability for such things.

So delete the line that says

mMediaRecorder.setOutputFormat();

and the error should disappear. And by the way. remove

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

what has already been done mMediaRecorder.setProfile().

+13
source

You may need to free the camera object before starting MediaRecorder with something like:

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

: setVideoEncoder setAudioEncoder .

+5

Source: https://habr.com/ru/post/1535145/


All Articles