Runtime exception on MediaRecorder.stop() function in android

515 Views Asked by At

I am creating a Screen Capturing App. Using MediaRecorder with MediaProjection API.

Getting Run-time Exception while stopping the recorder

Here is the code to stop Screen Capture

private void stop_recorder() {

    if (virtualDisplay == null) {
        return;
    }

    virtualDisplay.release();

    if (mediaProjection != null) {
        mediaProjection.unregisterCallback(projectionCallback);
        mediaProjection.stop();
        mediaProjection = null;
    }

    if (recorder != null) {
        recorder.stop();
        recorder.reset();

    }

}

The Exception occur is

E/MediaRecorder: stop failed: -1007 java.lang.RuntimeException: stop failed. at android.media.MediaRecorder.stop(Native Method)

1

There are 1 best solutions below

0
On

The Issue is with the emulator. It does not have audio and video source.

Look android.developer doc

RuntimeException is intentionally thrown to the application, if no valid audio/video data has been received when stop() is called. This happens if stop() is called immediately after start().

Solution

Put the mediaRecorder.stop() function in try-catch block