问题描述
当使用 Camera.PreviewCallback 实现时,在初始化相机并开始预览(Camera.startPrevew())后,onPreviewFrame 被毫无问题地调用.问题是,如果我使用 MediaRecorder 进行视频录制,则不会再调用 onPreviewFrame.
when using the Camera.PreviewCallback implementation the onPreviewFrame is called without problem after initializing camera and starting preview (Camera.startPrevew()). The problem is if I make a video recording using MediaRecorder onPreviewFrame does not get called any more.
我知道在使用MediaRecorder录制视频时会停止Camera.PreviewCallback,但为什么不能重新启动?
I understand that when using MediaRecorder to record video stops the Camera.PreviewCallback, but why can't it be restarted?
我已尝试重置相机预览回调 (setPreviewCallback(callback)) 并重新启动 startPreview,但是当我进行预览时,没有调用 onPreviewFrame.
I have tried resetting the camera preview callback (setPreviewCallback(callback)) and restarting startPreview, but while I have a preview there is no call to onPreviewFrame.
推荐答案
必须在surfaceChanged方法中调用setPreviewCallback,而不能只在surfaceCreated中调用.
You must call setPreviewCallback in the surfaceChanged method, not only in the surfaceCreated.
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
if (mHolder.getSurface() == null){
return;
}
try {
mCamera.stopPreview();
} catch (Exception e){
// ignore: tried to stop a non-existent preview
}
try {
mCamera.setPreviewCallback(this);
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
} catch (Exception e){
Log.d(TAG, "Error starting camera preview: " + e.getMessage());
}
}
这篇关于未调用相机 onPreviewFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!