'MediaRecorder Crash?

I am going to record video but it's not working

public void startRecord() {
    if (preVideo()) {
        mRecorder.start();
    }
    isRecording = true;
}

public boolean preVideo() {
    if (mCamera == null) {
        return false;
    }

    mRecorder = new MediaRecorder();
    mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    mRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);

    CamcorderProfile cpHigh = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
    mRecorder.setProfile(cpHigh);
    mRecorder.setOutputFile(createFile().toString());
    mRecorder.setVideoSize(mFrameWidth, mFrameHeight);

    return true;
}

and logcat;

FATAL EXCEPTION: main Process: pt.chambino.p.pulse, PID: 6578 java.lang.IllegalStateException at android.media.MediaRecorder.start(Native Method) at org.opencv.android.MyJavaCameraView.startRecord(MyJavaCameraView.java:375) at pt.chambino.p.pulse.App.onRecord(App.java:241) at pt.chambino.p.pulse.App.onOptionsItemSelected(App.java:213) at android.app.Activity.onMenuItemSelected(Activity.java:2600) at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:1019) at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735) at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152) at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874) at com.android.internal.view.menu.ActionMenuView.invokeItem(ActionMenuView.java:546) at com.android.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:119) at android.view.View.performClick(View.java:4569) at android.view.View$PerformClick.run(View.java:18553) at android.os.Handler.handleCallback(Handler.java:733) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:212) at android.app.ActivityThread.main(ActivityThread.java:5151) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:684) at dalvik.system.NativeStart.main(Native Method)



Solution 1:[1]

There are two ways you can trigger an IllegalStateException with MediaRecorder.start():

  • The recording input you're using is already in use
  • You've not prepared the MediaRecorder properly for recording.

Consider this state diagram from the documentation:

enter image description here

Discounting the first option as there's not enough information to confirm or deny it, if I were to compare the flow of your code, it looks like you're missing two steps:

  • setOutputFormat

  • prepare

You also have the video source set as your phone's screen rather than the camera, so I would change your code to be like this:

public void startRecord() {
    try {
        if (preVideo()) {
            mRecorder.start();
            isRecording = true;
        }
    } catch(IOException | IllegalStateException e) {
        e.printStackTrace();
        // Error handling
    }
}

public boolean preVideo() throws IllegalStateException, IOException {
    if (mCamera == null) {
        return false;
    }

    mRecorder = new MediaRecorder();
    mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    mRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
    mRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);

    CamcorderProfile cpHigh = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
    mRecorder.setProfile(cpHigh);
    mRecorder.setOutputFile(createFile().toString());
    mRecorder.setVideoSize(mFrameWidth, mFrameHeight);

    mRecorder.prepare();

    return true;
}

Solution 2:[2]

Replace:

mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);

With this:

mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Shubham Vyas