我目前正在使用camera2 api在服務中錄製視頻。攝像機服務記錄
我不能找到一種方法,當預覽面被破壞更新camerasession的輸出列表。換句話說:當一個表面被銷燬時(例如當活動在後臺進行時)如何進行以保持記錄並避免由於空表面造成的崩潰。
這是我如何開始視頻錄製。
private void startRecordingVideo() {
if (null == mCameraDevice || null == mPreviewSize) {
return;
}
try {
closePreviewSession();
setUpMediaRecorder();
mPreviewBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_RECORD);
List<Surface> surfaces = new ArrayList<>();
if(mTextureView !=null) {
SurfaceTexture texture = mTextureView.getSurfaceTexture();
assert texture != null;
texture.setDefaultBufferSize(mPreviewSize.getWidth(), mPreviewSize.getHeight()); // Set up Surface for the camera preview
previewSurface = new Surface(texture);
surfaces.add(previewSurface);
mPreviewBuilder.addTarget(previewSurface);
}
// Set up Surface for the MediaRecorder
Surface recorderSurface = mMediaRecorder.getSurface();
surfaces.add(recorderSurface);
mPreviewBuilder.addTarget(recorderSurface);
// Start a capture session
// Once the session starts, we can update the UI and start recording
mCameraDevice.createCaptureSession(surfaces, new CameraCaptureSession.StateCallback() {
@Override
public void onConfigured(@NonNull CameraCaptureSession cameraCaptureSession) {
mPreviewSession = cameraCaptureSession;
updatePreview();
mMediaRecorder.start();
}
@Override
public void onConfigureFailed(@NonNull CameraCaptureSession cameraCaptureSession) {
Timber.e("Error when creating captureSession");
}
}, mBackgroundHandler);
} catch (CameraAccessException | IOException e) {
e.printStackTrace();
}
}
當我將活動發送到後臺時:預覽表面被破壞,這是我奮鬥的地方。我應該如何更新我的capturesession,以便從會話輸出中刪除預覽表面而不停止捕獲進程?