Android video recording - IllegalStateException on mediaRecorder.start

Joshua Ohana

I am following the tutorial on https://developer.android.com/guide/topics/media/camera.html#capture-video

As such I follow the below order when trying to start the camera:

  1. Camera.open
  2. camera.unlock
  3. mediaRecorder.setCamera
  4. mediaRecorder.setAudioSource
  5. mediaRecorder.setVideoSource
  6. mediaRecorder.setProfile
  7. mediaRecorder.setOutputFile
  8. mediaRecorder.prepare
  9. mediaRecorder.start <- this is where I get the IllegalStateException

I can figure out what could be going wrong since I'm following the guide, running 5.0.2

private Camera mCamera;
private MediaRecorder mMediaRecorder;

public CameraActivity() {
    mCamera = getCameraInstance();
    mMediaRecorder = new MediaRecorder();
}

public static Camera getCameraInstance(){
    Camera c = null;
    try {
        c = Camera.open();
    }
    catch (Exception e) { ... }
    return c;
}

public void startRecording() {

    mCamera.unlock();
    mMediaRecorder.setCamera(mCamera);

    mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
    mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
    mMediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_LOW));
    mMediaRecorder.setOutputFile(getOutputMediaFile(MEDIA_TYPE_VIDEO).toString());

    try {
        mMediaRecorder.prepare();
    }
    catch (IOException e) { ... }
    catch (IllegalStateException e) { ... }

    try {
        mMediaRecorder.start();
    }
    catch (Exception e) {
        Log.d(TAG, "exception on mediaRecorder.start" + e.toString()); // This is the exception that gets thrown on .start
    }
}

My manifest includes all necessary permissions

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.RECORD_VIDEO" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.microphone" />

I have also tried manually setting format instead of using .setProfile, same results

    mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
    mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
    mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);

Update

The file is indeed created, though it's unplayable of course, so I know it's working up to that point. The call to prepare does not throw an exception, and occurs before start. Same exception thrown on start()

Edson Menegatti

Comparing agains my code, it seems you're missing two calls:

mediaRecorder.setVideoSize(int width, int height)
mediaRecorder.setPreviewDisplay(SurfaceHolder surfaceHolder)

The latter is most likely to be causing the crash as Android requires a valid preview surface to start recording. This is done so to prevent hidden cameras apps.

There are dozens of questions related to recording without a preview surface, but this one seems to sum up what you need to do to bypass this restriction. The basic idea is to resize your surface to be 1x1 and pass it to your mediaRecorder instance. Keep in mind that this may not work in all devices though.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Android video recording - IllegalStateException on mediaRecorder.start

From Dev

IllegalStateException in MediaRecorder.start()

From Dev

Android Video Orientation changes on mediarecorder.Start()

From Dev

Why is MediaRecorder delaying the start of recording?

From Dev

java.lang.IllegalStateException at android.media.MediaRecorder.start when calling MediaRecorder.start in onLongClickListener

From Dev

android record video without start recording button

From Dev

MediaRecorder.start() throwing IllegalStateException

From Dev

ANR on Mediarecorder onStop : Video Recording Camera API

From Dev

mediarecorder how to pause and resume recording video

From Dev

mediarecorder how to pause and resume recording video

From Dev

Auto start video recording

From Dev

Voice Call recording in android using MediaRecorder

From Dev

Android MediaRecorder/Player Either Not Recording or Not Playing

From Dev

Recording FLAC audio using AudioRecorder/MediaRecorder in Android

From Dev

Android - Recording Video in the Background

From Dev

Video recording error in android

From Dev

Android MediaRecorder. Continue recording after screen rotation

From Dev

Android MediaRecorder. Continue recording after screen rotation

From Dev

Not able to set manual video size in Android MediaRecorder

From Dev

Slow motion video preview with Android MediaRecorder

From Dev

Not able to set manual video size in Android MediaRecorder

From Dev

Android MediaRecorder start failed in invalid state 4

From Dev

Android: MediaRecorder.start() runetimException vague error

From Dev

Mediarecorder IllegalStateException on SetProfile

From Dev

Android: Mediarecoder is throwing IllegalStateException in start()

From Dev

Android Vine/Instagram style video recording

From Dev

How to embed text while recording video in Android?

From Dev

android video recording with duration limit and low quality

From Dev

Android video camera with TRRS external microphone recording

Related Related

  1. 1

    Android video recording - IllegalStateException on mediaRecorder.start

  2. 2

    IllegalStateException in MediaRecorder.start()

  3. 3

    Android Video Orientation changes on mediarecorder.Start()

  4. 4

    Why is MediaRecorder delaying the start of recording?

  5. 5

    java.lang.IllegalStateException at android.media.MediaRecorder.start when calling MediaRecorder.start in onLongClickListener

  6. 6

    android record video without start recording button

  7. 7

    MediaRecorder.start() throwing IllegalStateException

  8. 8

    ANR on Mediarecorder onStop : Video Recording Camera API

  9. 9

    mediarecorder how to pause and resume recording video

  10. 10

    mediarecorder how to pause and resume recording video

  11. 11

    Auto start video recording

  12. 12

    Voice Call recording in android using MediaRecorder

  13. 13

    Android MediaRecorder/Player Either Not Recording or Not Playing

  14. 14

    Recording FLAC audio using AudioRecorder/MediaRecorder in Android

  15. 15

    Android - Recording Video in the Background

  16. 16

    Video recording error in android

  17. 17

    Android MediaRecorder. Continue recording after screen rotation

  18. 18

    Android MediaRecorder. Continue recording after screen rotation

  19. 19

    Not able to set manual video size in Android MediaRecorder

  20. 20

    Slow motion video preview with Android MediaRecorder

  21. 21

    Not able to set manual video size in Android MediaRecorder

  22. 22

    Android MediaRecorder start failed in invalid state 4

  23. 23

    Android: MediaRecorder.start() runetimException vague error

  24. 24

    Mediarecorder IllegalStateException on SetProfile

  25. 25

    Android: Mediarecoder is throwing IllegalStateException in start()

  26. 26

    Android Vine/Instagram style video recording

  27. 27

    How to embed text while recording video in Android?

  28. 28

    android video recording with duration limit and low quality

  29. 29

    Android video camera with TRRS external microphone recording

HotTag

Archive