androidmediarecorder

MediaRecorder failed when i stop the recording


I have this error. Can somebody please help me, I think it's something about touch listener... The error is happening when I release my finger.

04-25 20:07:00.263: D/FB Sessions(18429): false
04-25 20:07:04.533: I/MediaRecorderJNI(18429): prepare: surface=0x189250 (identity=1813)
04-25 20:07:10.493: E/MediaRecorder(18429): stop failed: -1007
04-25 20:07:10.493: D/AndroidRuntime(18429): Shutting down VM
04-25 20:07:10.493: W/dalvikvm(18429): threadid=1: thread exiting with uncaught exception (group=0x40018608)
04-25 20:07:10.503: E/AndroidRuntime(18429): FATAL EXCEPTION: main
04-25 20:07:10.503: E/AndroidRuntime(18429): java.lang.RuntimeException: stop failed.
04-25 20:07:10.503: E/AndroidRuntime(18429):    at android.media.MediaRecorder.stop(Native Method)
04-25 20:07:10.503: E/AndroidRuntime(18429):    at com.crewbase.rec.RecordActivity.stopRecording(RecordActivity.java:151)
04-25 20:07:10.503: E/AndroidRuntime(18429):    at com.crewbase.rec.RecordActivity.access$2(RecordActivity.java:150)
04-25 20:07:10.503: E/AndroidRuntime(18429):    at com.crewbase.rec.RecordActivity$1.onTouch(RecordActivity.java:79)
04-25 20:07:10.503: E/AndroidRuntime(18429):    at android.view.View.dispatchTouchEvent(View.java:3897)
04-25 20:07:10.503: E/AndroidRuntime(18429):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
04-25 20:07:10.503: E/AndroidRuntime(18429):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
04-25 20:07:10.503: E/AndroidRuntime(18429):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
04-25 20:07:10.503: E/AndroidRuntime(18429):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
04-25 20:07:10.503: E/AndroidRuntime(18429):    at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1737)
04-25 20:07:10.503: E/AndroidRuntime(18429):    at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1153)
04-25 20:07:10.503: E/AndroidRuntime(18429):    at android.app.Activity.dispatchTouchEvent(Activity.java:2096)
04-25 20:07:10.503: E/AndroidRuntime(18429):    at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1721)
04-25 20:07:10.503: E/AndroidRuntime(18429):    at android.view.ViewRoot.deliverPointerEvent(ViewRoot.java:2200)
04-25 20:07:10.503: E/AndroidRuntime(18429):    at android.view.ViewRoot.handleMessage(ViewRoot.java:1884)
04-25 20:07:10.503: E/AndroidRuntime(18429):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-25 20:07:10.503: E/AndroidRuntime(18429):    at android.os.Looper.loop(Looper.java:130)
04-25 20:07:10.503: E/AndroidRuntime(18429):    at android.app.ActivityThread.main(ActivityThread.java:3835)
04-25 20:07:10.503: E/AndroidRuntime(18429):    at java.lang.reflect.Method.invokeNative(Native Method)
04-25 20:07:10.503: E/AndroidRuntime(18429):    at java.lang.reflect.Method.invoke(Method.java:507)
04-25 20:07:10.503: E/AndroidRuntime(18429):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
04-25 20:07:10.503: E/AndroidRuntime(18429):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
04-25 20:07:10.503: E/AndroidRuntime(18429):    at dalvik.system.NativeStart.main(Native Method)

And that is happening when I try to run this code:

from touch listener:

/// Preview is SurfaceView in my view
preview.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:

                prepareRecording();
                break;
            case MotionEvent.ACTION_MOVE:
            //Log.d(TAG, String.format("ACTION_MOVE | x:%s y:%s", 
                break;
            case MotionEvent.ACTION_UP:
                stopRecording();
                break;
            }
            return true;
        }
    });

And these two methods:

private void prepareRecording() {
    try {
        camera.unlock();

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

        File tempFile = new File(Environment.getExternalStorageDirectory(), "/rec/temp/video_" + String.valueOf(videoCount) + ".mp4");

        recorder.setOutputFile(tempFile.getPath());
        recorder.setVideoFrameRate(25);
        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
        recorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
        recorder.setPreviewDisplay(holder.getSurface());

        recorder.prepare();
        recorder.start();
    }  catch (IllegalStateException e) {
        Log.e("REDORDING :: ",e.getMessage());
        e.printStackTrace();
    } catch (IOException e) {
        Log.e("REDORDING :: ",e.getMessage());
        e.printStackTrace();
    }
}

private void stopRecording() {
    recorder.stop();
    camera.lock();
}

Solution

  • Look at the documentation:

    Note that a 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(). The failure lets the application take action accordingly to clean up the output file (delete the output file, for instance), since the output file is not properly constructed when this happens.

    In other words: Dalvik throws the exception on purpose. You have to handle it to clean up after your app. You'd have to handle it like this:

    private void stopRecording() {
        try {
            recorder.stop();
        } catch(RuntimeException stopException) {
            // handle cleanup here
        }
        camera.lock();
    }