I am doing video recording with media recorder.
For this i used below code .
private void prepareMediaRecorder(boolean vsize) {
mrec = new MediaRecorder();
mrec.setOnErrorListener(new OnErrorListener() {
@Override
public void onError(MediaRecorder mr, int what, int extra) {
// TODO Auto-generated method stub
if (extra == -1007)
{
prepareMediaRecorder(false);
}
else
{
unableToRecord();
}
}
});
camera.lock();
camera.unlock();
mrec.setCamera(camera);
mrec.setAudioSource(MediaRecorder.AudioSource.MIC);
mrec.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mrec.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mrec.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mrec.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
if (vsize)
mrec.setVideoSize(getMaxSupportedVideoSize().width,
getMaxSupportedVideoSize().height);
else
mrec.setVideoSize(640, 480);
mrec.setOutputFile(path + filename);
mrec.setMaxDuration(30000);
mrec.setPreviewDisplay(surfaceHolder.getSurface());
if (!onlyback
&& currentapiVersion >= android.os.Build.VERSION_CODES.GINGERBREAD) {
if (open_camera == 1)
mrec.setOrientationHint(270);
else
mrec.setOrientationHint(90);
} else if (currentapiVersion >= android.os.Build.VERSION_CODES.GINGERBREAD) {
mrec.setOrientationHint(90);
}
try {
mrec.prepare();
mrec.start();
} catch (Exception e) {
e.printStackTrace();
}
}
In the above code, when media recorder error listener called I am recreating media recorder with other video size but while doing this I am getting camera lock exception getting.
how can I solve this?
This is how I tackle the problem of attempting to use certain video recording sizes that may or may not be available on different devices. I start by attempting to record with an ideal resolution, and use try-catch blocks in order to shut down and retry opening the video camera at a different resolution. This will likely not be a huge issue with future versions of android due to increased support for
CamcorderProfiles, but as of now there are many devices that simply do not accurately share available video resolutions.I call the
initializeVideoRecorderfn from an activity and I use a custom class I created called aPreviewSize, which is basically just a width and height neatly packaged. I defined these constantPreviewSizes above.I think the important thing is to release everything and retry prior to attempting to connect to a different preview size. I do this using the
releaseVideoCamerafunction. You are also callinglockprior tounlockwhich seems potentially problematic (maybe not though).Anyways here is my code. I have removed some portions, and left out certain functions unrelated to your needs: