如何在屏幕关闭后继续捕获视频?

时间:2016-03-09 08:14:33

标签: android camera

在我的应用程序中,我使用MediaRecorder捕获视频。 但是当我在应用程序录制时关闭屏幕时,生成的视频当时是黑色的。有没有办法防止这种情况并使相机继续记录?这是我的代码:

CameraManager manager = (CameraManager) getSystemService(CAMERA_SERVICE);


mMediaRecorder = new MediaRecorder();
outputFile = new File(dir, "temp");
try {
    String[] ids = manager.getCameraIdList();
    CameraCharacteristics characteristics = manager.getCameraCharacteristics(ids[0]);
    StreamConfigurationMap configs = characteristics.get(
            CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
    Size[] sizes = configs.getOutputSizes(MediaCodec.class);
    final Size sizeHigh = sizes[0];
    manager.openCamera(ids[0], new CameraDevice.StateCallback() {
        @Override
        public void onOpened(CameraDevice camera) {
            mCamera = camera;
            mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
            mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
            mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
            mMediaRecorder.setVideoSize(sizeHigh.getWidth(), sizeHigh.getHeight());
            mMediaRecorder.setMaxFileSize(0);


            try {
                mMediaRecorder.setOutputFile(outputFile.getAbsolutePath());
                mMediaRecorder.prepare();
                List<Surface> list = new ArrayList<>();
                list.add(mMediaRecorder.getSurface());
                final CaptureRequest.Builder captureRequest = mCamera.createCaptureRequest(CameraDevice.TEMPLATE_RECORD);
                captureRequest.addTarget(mMediaRecorder.getSurface());
                mCaptureRequest = captureRequest.build();
                mCamera.createCaptureSession(list, new CameraCaptureSession.StateCallback() {
                    @Override
                    public void onConfigured(CameraCaptureSession session) {
                        mSession = session;
                        mMediaRecorder.start();
                        try {
                            mSession.setRepeatingRequest(mCaptureRequest,
                                    new CameraCaptureSession.CaptureCallback() {
                                        @Override
                                        public void onCaptureStarted(CameraCaptureSession session, CaptureRequest request, long timestamp, long frameNumber) {
                                            super.onCaptureStarted(session, request, timestamp, frameNumber);
                                                if(frameNumber > 2*60*25) {
                                                    mSession.stopRepeating();
                                                    mMediaRecorder.stop();
                                                    mSession=null;
                                                    mCamera.close();
                                                }
                                            }



                                            @Override
                                            public void onCaptureCompleted(CameraCaptureSession session, CaptureRequest request, TotalCaptureResult result) {
                                                super.onCaptureCompleted(session, request, result);
                                            }
                                        }, null);
                            } catch (CameraAccessException e) {
                                e.printStackTrace();
                            }

                        }


                        @Override
                        public void onConfigureFailed(CameraCaptureSession session) {
                            mSession = session;
                        }
                    }, null);
                } catch (Exception e) {
                    e.printStackTrace();
                }

0 个答案:

没有答案