Android 准备 videoRecorder 停止预览

Android prepare videoRecorder stops preview

所以我有一个相机应用程序,我也想录制视频。 当我按下按钮从相机切换到视频时,我会这样做

public void prepareVideoRecorder() {
    mCamera.stopPreview();
    mCamera.prepareRecorder(video_orientation, camera_surface);
    // mCamera.startPreview();

}

和我的准备方法

public void prepareRecorder(int video_degrees, CameraSurfaceView sf) {
    CamcorderProfile camcorderProfile = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);

    recorder = new MediaRecorder();
    mCamera.unlock();
    recorder.setCamera(mCamera);
    recorder.setPreviewDisplay(sf.getHolder().getSurface());

    Log.v("this", "orientation hint = " + video_degrees);
    recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
    recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
    recorder.setOrientationHint(video_degrees);

    File imageFileFolder = new File(Environment.getExternalStorageDirectory(), "DCIM");
    String filename = imageFileFolder.toString() + "/" + new SimpleDateFormat("ddMMyyyyHHmmss").format(new Date()) + ".mp4";
    recorder.setOutputFile(filename);

    recorder.setProfile(camcorderProfile);
    try {
        recorder.prepare();
    } catch (Exception e) {
        e.printStackTrace();
    }

}

当我按下 "switch" 按钮时,预览停止。但是,如果我按下开始录制按钮,一切正常,我可以录制视频并且预览再次开始。 如果我尝试从 prepareVideoRecorder 中取消对 mCamera.startPreview() 的注释,我会收到此错误

    02-24 13:44:49.146: E/InputEventReceiver(18078): Exception dispatching input event.
02-24 13:44:49.146: E/MessageQueue-JNI(18078): Exception in MessageQueue callback: handleReceiveCallback
02-24 13:44:49.146: E/MessageQueue-JNI(18078): java.lang.RuntimeException: startPreview failed
02-24 13:44:49.146: E/MessageQueue-JNI(18078):  at android.hardware.Camera.startPreview(Native Method)
02-24 13:44:49.146: E/MessageQueue-JNI(18078):  at org.dtaz.yoda.DtazCamera.startPreview(DtazCamera.java:136)
02-24 13:44:49.146: E/MessageQueue-JNI(18078):  at org.dtaz.yoda.DtazPreview.prepareVideoRecorder(DtazPreview.java:343)
02-24 13:44:49.146: E/MessageQueue-JNI(18078):  at org.dtaz.yoda.CameraActivity.swipeGesture(CameraActivity.java:600)
02-24 13:44:49.146: E/MessageQueue-JNI(18078):  at org.dtaz.yoda.CameraSurfaceView.onTouchEvent(CameraSurfaceView.java:87)
02-24 13:44:49.146: E/MessageQueue-JNI(18078):  at android.view.View.dispatchTouchEvent(View.java:7706)
02-24 13:44:49.146: E/MessageQueue-JNI(18078):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
02-24 13:44:49.146: E/MessageQueue-JNI(18078):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
02-24 13:44:49.146: E/MessageQueue-JNI(18078):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
02-24 13:44:49.146: E/MessageQueue-JNI(18078):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
02-24 13:44:49.146: E/MessageQueue-JNI(18078):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
02-24 13:44:49.146: E/MessageQueue-JNI(18078):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
02-24 13:44:49.146: E/MessageQueue-JNI(18078):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
02-24 13:44:49.146: E/MessageQueue-JNI(18078):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
02-24 13:44:49.146: E/MessageQueue-JNI(18078):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
02-24 13:44:49.146: E/MessageQueue-JNI(18078):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
02-24 13:44:49.146: E/MessageQueue-JNI(18078):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
02-24 13:44:49.146: E/MessageQueue-JNI(18078):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
02-24 13:44:49.146: E/MessageQueue-JNI(18078):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
02-24 13:44:49.146: E/MessageQueue-JNI(18078):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
02-24 13:44:49.146: E/MessageQueue-JNI(18078):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
02-24 13:44:49.146: E/MessageQueue-JNI(18078):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
02-24 13:44:49.146: E/MessageQueue-JNI(18078):  at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2333)
02-24 13:44:49.146: E/MessageQueue-JNI(18078):  at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1568)
02-24 13:44:49.146: E/MessageQueue-JNI(18078):  at android.app.Activity.dispatchTouchEvent(Activity.java:2468)
02-24 13:44:49.146: E/MessageQueue-JNI(18078):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2281)
02-24 13:44:49.146: E/MessageQueue-JNI(18078):  at android.view.View.dispatchPointerEvent(View.java:7886)
02-24 13:44:49.146: E/MessageQueue-JNI(18078):  at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:3947)
02-24 13:44:49.146: E/MessageQueue-JNI(18078):  at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3826)
02-24 13:44:49.146: E/MessageQueue-JNI(18078):  at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3392)
02-24 13:44:49.146: E/MessageQueue-JNI(18078):  at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3442)
02-24 13:44:49.146: E/MessageQueue-JNI(18078):  at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3411)
02-24 13:44:49.146: E/MessageQueue-JNI(18078):  at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3518)
02-24 13:44:49.146: E/MessageQueue-JNI(18078):  at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3419)
02-24 13:44:49.146: E/MessageQueue-JNI(18078):  at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3575)
02-24 13:44:49.146: E/MessageQueue-JNI(18078):  at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3392)
02-24 13:44:49.146: E/MessageQueue-JNI(18078):  at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3442)
02-24 13:44:49.146: E/MessageQueue-JNI(18078):  at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3411)
02-24 13:44:49.146: E/MessageQueue-JNI(18078):  at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3419)
02-24 13:44:49.146: E/MessageQueue-JNI(18078):  at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3392)
02-24 13:44:49.146: E/MessageQueue-JNI(18078):  at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5532)
02-24 13:44:49.146: E/MessageQueue-JNI(18078):  at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5512)
02-24 13:44:49.146: E/MessageQueue-JNI(18078):  at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5483)
02-24 13:44:49.146: E/MessageQueue-JNI(18078):  at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5612)
02-24 13:44:49.146: E/MessageQueue-JNI(18078):  at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
02-24 13:44:49.146: E/MessageQueue-JNI(18078):  at android.os.MessageQueue.nativePollOnce(Native Method)
02-24 13:44:49.146: E/MessageQueue-JNI(18078):  at android.os.MessageQueue.next(MessageQueue.java:138)
02-24 13:44:49.146: E/MessageQueue-JNI(18078):  at android.os.Looper.loop(Looper.java:123)
02-24 13:44:49.146: E/MessageQueue-JNI(18078):  at android.app.ActivityThread.main(ActivityThread.java:5034)
02-24 13:44:49.146: E/MessageQueue-JNI(18078):  at java.lang.reflect.Method.invokeNative(Native Method)
02-24 13:44:49.146: E/MessageQueue-JNI(18078):  at java.lang.reflect.Method.invoke(Method.java:515)
02-24 13:44:49.146: E/MessageQueue-JNI(18078):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
02-24 13:44:49.146: E/MessageQueue-JNI(18078):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:611)
02-24 13:44:49.146: E/MessageQueue-JNI(18078):  at dalvik.system.NativeStart.main(Native Method)
02-24 13:44:49.156: D/AndroidRuntime(18078): Shutting down VM
02-24 13:44:49.156: W/dalvikvm(18078): threadid=1: thread exiting with uncaught exception (group=0x41841c08)
02-24 13:44:49.166: E/AndroidRuntime(18078): FATAL EXCEPTION: main
02-24 13:44:49.166: E/AndroidRuntime(18078): Process: org.dtaz.yoda, PID: 18078
02-24 13:44:49.166: E/AndroidRuntime(18078): java.lang.RuntimeException: startPreview failed
02-24 13:44:49.166: E/AndroidRuntime(18078):    at android.hardware.Camera.startPreview(Native Method)
02-24 13:44:49.166: E/AndroidRuntime(18078):    at org.dtaz.yoda.DtazCamera.startPreview(DtazCamera.java:136)
02-24 13:44:49.166: E/AndroidRuntime(18078):    at org.dtaz.yoda.DtazPreview.prepareVideoRecorder(DtazPreview.java:343)
02-24 13:44:49.166: E/AndroidRuntime(18078):    at org.dtaz.yoda.CameraActivity.swipeGesture(CameraActivity.java:600)
02-24 13:44:49.166: E/AndroidRuntime(18078):    at org.dtaz.yoda.CameraSurfaceView.onTouchEvent(CameraSurfaceView.java:87)
02-24 13:44:49.166: E/AndroidRuntime(18078):    at android.view.View.dispatchTouchEvent(View.java:7706)
02-24 13:44:49.166: E/AndroidRuntime(18078):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
02-24 13:44:49.166: E/AndroidRuntime(18078):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
02-24 13:44:49.166: E/AndroidRuntime(18078):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
02-24 13:44:49.166: E/AndroidRuntime(18078):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
02-24 13:44:49.166: E/AndroidRuntime(18078):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
02-24 13:44:49.166: E/AndroidRuntime(18078):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
02-24 13:44:49.166: E/AndroidRuntime(18078):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
02-24 13:44:49.166: E/AndroidRuntime(18078):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
02-24 13:44:49.166: E/AndroidRuntime(18078):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
02-24 13:44:49.166: E/AndroidRuntime(18078):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
02-24 13:44:49.166: E/AndroidRuntime(18078):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
02-24 13:44:49.166: E/AndroidRuntime(18078):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
02-24 13:44:49.166: E/AndroidRuntime(18078):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
02-24 13:44:49.166: E/AndroidRuntime(18078):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
02-24 13:44:49.166: E/AndroidRuntime(18078):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
02-24 13:44:49.166: E/AndroidRuntime(18078):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
02-24 13:44:49.166: E/AndroidRuntime(18078):    at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2333)
02-24 13:44:49.166: E/AndroidRuntime(18078):    at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1568)
02-24 13:44:49.166: E/AndroidRuntime(18078):    at android.app.Activity.dispatchTouchEvent(Activity.java:2468)
02-24 13:44:49.166: E/AndroidRuntime(18078):    at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2281)
02-24 13:44:49.166: E/AndroidRuntime(18078):    at android.view.View.dispatchPointerEvent(View.java:7886)
02-24 13:44:49.166: E/AndroidRuntime(18078):    at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:3947)
02-24 13:44:49.166: E/AndroidRuntime(18078):    at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3826)
02-24 13:44:49.166: E/AndroidRuntime(18078):    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3392)
02-24 13:44:49.166: E/AndroidRuntime(18078):    at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3442)
02-24 13:44:49.166: E/AndroidRuntime(18078):    at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3411)
02-24 13:44:49.166: E/AndroidRuntime(18078):    at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3518)
02-24 13:44:49.166: E/AndroidRuntime(18078):    at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3419)
02-24 13:44:49.166: E/AndroidRuntime(18078):    at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3575)
02-24 13:44:49.166: E/AndroidRuntime(18078):    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3392)
02-24 13:44:49.166: E/AndroidRuntime(18078):    at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3442)
02-24 13:44:49.166: E/AndroidRuntime(18078):    at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3411)
02-24 13:44:49.166: E/AndroidRuntime(18078):    at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3419)
02-24 13:44:49.166: E/AndroidRuntime(18078):    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3392)
02-24 13:44:49.166: E/AndroidRuntime(18078):    at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5532)
02-24 13:44:49.166: E/AndroidRuntime(18078):    at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5512)
02-24 13:44:49.166: E/AndroidRuntime(18078):    at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5483)
02-24 13:44:49.166: E/AndroidRuntime(18078):    at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5612)
02-24 13:44:49.166: E/AndroidRuntime(18078):    at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
02-24 13:44:49.166: E/AndroidRuntime(18078):    at android.os.MessageQueue.nativePollOnce(Native Method)
02-24 13:44:49.166: E/AndroidRuntime(18078):    at android.os.MessageQueue.next(MessageQueue.java:138)
02-24 13:44:49.166: E/AndroidRuntime(18078):    at android.os.Looper.loop(Looper.java:123)
02-24 13:44:49.166: E/AndroidRuntime(18078):    at android.app.ActivityThread.main(ActivityThread.java:5034)
02-24 13:44:49.166: E/AndroidRuntime(18078):    at java.lang.reflect.Method.invokeNative(Native Method)
02-24 13:44:49.166: E/AndroidRuntime(18078):    at java.lang.reflect.Method.invoke(Method.java:515)
02-24 13:44:49.166: E/AndroidRuntime(18078):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
02-24 13:44:49.166: E/AndroidRuntime(18078):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:611)
02-24 13:44:49.166: E/AndroidRuntime(18078):    at dalvik.system.NativeStart.main(Native Method)

有人知道这是怎么回事吗?

好的所以我想出了一个解决方案,我不知道它是否正确但它有效。

情况是,为了录制视频,相机实例需要由 MediaRecorder 获取,同时如果要显示预览,相机实例需要位于表面视图中。这就是为什么如果您开始录制,您可以看到已捕获的图像。

所以解决这个问题的唯一方法是像这样将准备放在 mediarecorder.start() 之前的 onclick 方法中。

take_picture.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            if (isVideo) {
                if (!isRecording) {
                    camera_preview.prepareVideoRecorder();
                    camera_preview.startRecording();
                    take_picture.setText("stop");
                    isRecording = true;
                } else {
                    camera_preview.stopRecording();
                    take_picture.setText("start");
                    isRecording = false;
                }
            } 
        }
    }); 

如果你这样写代码,一切正常。如果有人有更好的解决方案请 post 以便我们可以使用它!