我正在为我正在制作的应用创建视频捕捉活动,我也希望捕捉预览帧以用作播放按钮。我已将SurfaceView
设置为onCreate(...)
,并将相机的预览显示连接到SurfaceHolder.Callback
:
private Callback mSurfaceViewCallback = new Callback() {
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
mSurfaceCreated = false;
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
try {
mSurfaceCreated = true;
mCamera.setPreviewDisplay(mHolder);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
try {
mSurfaceCreated = true;
mCamera.setPreviewDisplay(mHolder);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
适用于静态相机应用程序。在onPause()
和onResume()
:
@Override
protected void onResume() {
super.onResume();
if(mCamera!=null)
mCamera.startPreview();
}
@Override
protected void onPause() {
if(mCamera!=null)
mCamera.stopPreview();
super.onPause();
}
我已将MediaRecorder
设置为这样:
protected void startRecording() {
mMediaRecorder = new MediaRecorder();
mCamera.unlock();
mMediaRecorder.setCamera(mCamera);
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mMediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_LOW));
mMediaRecorder.setMaxDuration(10000);
mMediaRecorder.setOutputFile(new File(getFilesDir(), mChallengeUUID + ".mp4").getAbsolutePath());
mMediaRecorder.setPreviewDisplay(mHolder.getSurface());
try {
mMediaRecorder.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mCamera.setOneShotPreviewCallback(mPreviewCallback);
mMediaRecorder.start();
}
然而,倒数第二行不起作用。我错过了Camera.PreviewCallback
的{{1}}方法。
正如我所说,拥有它的目的是从录制视频开始到用户显示帧。为什么回调没有被击中?
答案 0 :(得分:1)
mCamera.unlock()
将相机从过程中分离出来。有必要致电:
mCamera.reconnect();
致电之前:
mCamera.setOneShotPreviewCallback(mPreviewCallback);
(但在其他一切之后)。