Search code examples
androidandroid-mediarecorder

Unable to record video with media recorder android


I am trying to record video using media recorder. When ever i try to record video it crashed with the following logcat.

05-20 14:28:43.432: E/MediaRecorder(4281): start failed: -38 05-20 14:28:43.432: D/AndroidRuntime(4281): Shutting down VM 05-20 14:28:43.432: W/dalvikvm(4281): threadid=1: thread exiting with uncaught exception (group=0x415c5d88) 05-20 14:28:43.462: E/AndroidRuntime(4281): FATAL EXCEPTION: main 05-20 14:28:43.462: E/AndroidRuntime(4281): Process: com.abc.def, PID: 4281 05-20 14:28:43.462: E/AndroidRuntime(4281): java.lang.IllegalStateException 05-20 14:28:43.462: E/AndroidRuntime(4281): at android.media.MediaRecorder.start(Native Method) 05-20 14:28:43.462: E/AndroidRuntime(4281): at com.abc.def.PrivateChatDialog.startVideoRecording(PrivateChatDialog.java:1413) 05-20 14:28:43.462: E/AndroidRuntime(4281): at com.abc.def.PrivateChatDialog$3.onClick(PrivateChatDialog.java:198) 05-20 14:28:43.462: E/AndroidRuntime(4281): at android.view.View.performClick(View.java:4569) 05-20 14:28:43.462: E/AndroidRuntime(4281): at android.view.View$PerformClick.run(View.java:18570) 05-20 14:28:43.462: E/AndroidRuntime(4281): at android.os.Handler.handleCallback(Handler.java:743) 05-20 14:28:43.462: E/AndroidRuntime(4281): at android.os.Handler.dispatchMessage(Handler.java:99) 05-20 14:28:43.462: E/AndroidRuntime(4281): at android.os.Looper.loop(Looper.java:136) 05-20 14:28:43.462: E/AndroidRuntime(4281): at android.app.ActivityThread.main(ActivityThread.java:5212) 05-20 14:28:43.462: E/AndroidRuntime(4281): at java.lang.reflect.Method.invokeNative(Native Method) 05-20 14:28:43.462: E/AndroidRuntime(4281): at java.lang.reflect.Method.invoke(Method.java:515) 05-20 14:28:43.462: E/AndroidRuntime(4281): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 05-20 14:28:43.462: E/AndroidRuntime(4281): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602) 05-20 14:28:43.462: E/AndroidRuntime(4281): at dalvik.system.NativeStart.main(Native Method)

This is the code i am using to capture video.

protected void startVideoRecording() throws IOException {
        if(mrec==null)
        mrec = new MediaRecorder(); // Works well
        mCamera.unlock();
        mrec.setCamera(mCamera);
        mrec.setVideoSource(MediaRecorder.VideoSource.CAMERA);
        mrec.setAudioSource(MediaRecorder.AudioSource.MIC);
      mrec.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_LOW));
        mrec.setPreviewDisplay(surfaceHolder.getSurface());
        mrec.setOutputFile(getVideoFileName());
        mrec.setVideoSize(320, 280);
        mrec.prepare();
        mrec.start();
        videoRecording = true;
    }

I have tried a lot and searched a lot but could not find the solution. What I found was this SO question

But I do not have any background service recording audio. Please help me find a solution. Thank you.


Solution

  • I modified the function as follows:

    public void startVideoRecording() throws IOException {
            mrec = new MediaRecorder(); // Works well
            mCamera.unlock();
            mrec.setCamera(mCamera);
            mrec.setVideoSource(MediaRecorder.VideoSource.CAMERA);
            mrec.setAudioSource(MediaRecorder.AudioSource.MIC);
            mrec.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
            mrec.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
            mrec.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
            mrec.setPreviewDisplay(surfaceHolder.getSurface());
            mrec.setOrientationHint(90);
            String path = getVideoFileName();
            mrec.setOutputFile(path);
            mrec.prepare();
            mrec.start();
            videoRecording = true;
        }
    

    After this also the app crashed, then a restart of phone did the trick, now everything works well.