14

Want to build an app which keeps recording in background, is it possible ?

DraD
  • 211
  • 1
  • 3
  • 7
  • well i'm not sure about that, but what i know is that u can use the recording method on the onPause() method so when your is on the background will keep recording. and btw what have you tried ? – Kosh Feb 20 '13 at 09:44
  • think it is possible and u hav done the same what if another app or default camera app requests for camera? it will show camera already in use ?? doesnt it seam spying is not a good habit? – Athul Harikumar Feb 20 '13 at 09:45

1 Answers1

14

yes its very well possible.

Create an 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.

Check some rough code which will start & stop recording using camera, this you can call from your background service and will work smoothly.

    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;
    }
}

This is sample code, you need to add your own logic around and also handle exceptions accordingly.

Pawan Maheshwari
  • 15,088
  • 1
  • 48
  • 50