无法使用媒体录像机录制视频 android

Unable to record video with media recorder android

我正在尝试使用媒体录像机录制视频。 每当我尝试录制视频时,它都会因以下 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.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)

这是我用来捕捉视频的代码。

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

我尝试了很多,也搜索了很多,但没有找到解决办法。 我发现了什么 was this SO question

但是我没有任何后台录音服务。 请帮我找到解决办法。 谢谢。

我修改了函数如下:

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

在此之后应用程序也崩溃了,然后重新启动 phone 成功了,现在一切正常。