MediaRecorder failed when i stop the recording

此生再无相见时 提交于 2019-11-27 03:03:23

问题


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

回答1:


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



回答2:


I had a similar error -1007 when I was recording audio with AMR_WB, but it turned out that the problem was that I forgot to set sampling rate.

mediaRecorder.setAudioSamplingRate(16000);


来源:https://stackoverflow.com/questions/16221866/mediarecorder-failed-when-i-stop-the-recording

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!