How to use Android Camera in Background?

后端 未结 2 2008
感情败类
感情败类 2020-12-16 01:15

I\'m currently developing an app which need to record video in background and process it.

(It needs to get camera preview data real-time in background and have to im

相关标签:
2条回答
  • 2020-12-16 01:51

    You can use a service to start your camera in background. You can refer to this . Hope this will helps you.

    0 讨论(0)
  • 2020-12-16 01:56

    Yes it is possible with following steps..

    Create one activity which will start your background service on some event or you can also use alarm manager to start and stop the service as per your requirement.

    See the below code that'll help you.

    public boolean starMediaRecording(){
            Camera.Parameters params = mServiceCamera.getParameters();
            mServiceCamera.setParameters(params);
            Camera.Parameters p = mServiceCamera.getParameters();
    
            final List<Size> listSize = p.getSupportedPreviewSizes();
            Size mPreviewSize = listSize.get(2);
            p.setPreviewSize(mPreviewSize.width, mPreviewSize.height);
            p.setPreviewFormat(PixelFormat.YCbCr_420_SP);
            mServiceCamera.setParameters(p);
    
            try {
                mServiceCamera.setPreviewDisplay(mSurfaceHolder);
                mServiceCamera.startPreview();
            }
            catch (IOException e) {
                Log.e(TAG, e.getMessage());
                e.printStackTrace();
            }
    
            mServiceCamera.unlock();
    
            mMediaRecorder = new MediaRecorder();
            mMediaRecorder.setCamera(mServiceCamera);
            mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
            mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
            mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
            mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
            mMediaRecorder.setOutputFile("/sdcard/filenamevideo.mp4");
            mMediaRecorder.setVideoFrameRate(30);
            mMediaRecorder.setVideoSize(mPreviewSize.width, mPreviewSize.height);
            mMediaRecorder.setPreviewDisplay(mSurfaceHolder.getSurface());
    
            mMediaRecorder.prepare();
            mMediaRecorder.start(); 
    
            mRecordingStatus = true;
    
            return true;
    
    }
    
    public void stopMediaRecorder() {
        mServiceCamera.reconnect();
    
        mMediaRecorder.stop();
        mMediaRecorder.reset();
    
        mServiceCamera.stopPreview();
        mMediaRecorder.release();
    
        mServiceCamera.release();
        mServiceCamera = null;
        }
    }
    
    0 讨论(0)
提交回复
热议问题