我有一个带SurfaceView的活动,我想在这个活动中开始和停止视频录制,但是如果我进入其他活动,我希望继续记录视频。现在我开始录制视频(在活动1中),然后进入另一个活动,当我回到活动1并想要停止录制视频时,我的应用程序在调用recorder.stop();
时冻结了。我该怎么办?
初始化SurfaceView和SurfaceHolder:
SurfaceView cameraView = (SurfaceView) findViewById(R.id.surfaceView);
SurfaceHolder holder = cameraView.getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
开始录制视频:
protected void startRecordVideo() {
if (cam == null) {
cam = Camera.open();
try {
cam.setPreviewDisplay(holder);
} catch (IOException e) {
e.printStackTrace();
}
}
recording = true;
recorder = new MediaRecorder();
recorder.setCamera(cam);
initRecorder();
prepareRecorder();
try {
cam.unlock();
} catch (RuntimeException e) {
e.printStackTrace();
}
recorder.start();
}
停止录制视频(这会导致应用程序冻结):
protected void stopRecordVideo() { //cam - Camera, recorder - MediaRecorder
mc.IS_RECORD = false;
recording = false;
try {
cam.lock();
recorder.stop(); //app freezes in this line (i used Log)
recorder = null;
cam.reconnect();
cam.release();
cam = null;
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (RuntimeException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
附言:我不使用 camera.startPreview();
和 camera.stopPreview();