Want to build an app which keeps recording in background, is it possible ?
Asked
Active
Viewed 2.6k times
14
-
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 Answers
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
-
thanks for quick reply. I tried this and it worked after some minor changes. – DraD Feb 21 '13 at 08:21
-
6what is mSurfaceHolder in your example ? If I'm using a empty one i get error on some device, and i cant really get one if i'm only using a service – Yalla T. Feb 21 '13 at 08:42
-
3This code works fine on Android 2.3.3 HTC Desire, but background recording fails on Galaxy S3 and Nexus 4. Any ideas? – Sergii Mar 23 '13 at 22:18
-
2Ditto on the mSurfaceHolder. That's my main problem, figuring out where to get that in a Service. – Rahat Ahmed Mar 24 '13 at 23:30
-
3what is "mServiceCamera" in this method? DraD can you please tell me what changes you have done exactly? – Pratik Dasa Sep 16 '13 at 11:50
-
What's the point of getting the camera parameters only to set them and get them again? – Sam Nov 15 '14 at 23:21
-
-
First two commands in function `starMediaRecording` is useless right? – Peter Zhu Mar 17 '15 at 11:05
-