I'm trying to create a simple video recorder and here's my code in order that I'm calling it:
protected boolean prepareForVideoRecording() {
try {
mCamera.unlock();
mMediaRecorder = new MediaRecorder();
mMediaRecorder.setCamera(mCamera);
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mMediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));
mMediaRecorder.setOutputFile(getOutputMediaFile1(MEDIA_TYPE_VIDEO).toString());
mMediaRecorder.setPreviewDisplay(mPreview.getHolder().getSurface());
try {
mMediaRecorder.prepare();
Toast.makeText(getContextOfMainFragment(), "Prepared!", Toast.LENGTH_SHORT).show();
} catch (IllegalStateException e) {
Log.e(TAG,"IllegalStateException when preparing MediaRecorder "+ e.getMessage());
e.getStackTrace();
releaseMediaRecorder();
return false;
} catch (IOException e) {
Log.e(TAG,"IOException when preparing MediaRecorder "+ e.getMessage());
e.getStackTrace();
releaseMediaRecorder();
return false;
}
return true;
} catch (Exception e) {
return false;
}
}
Please ignore the toast messages, I kinda use them sometimes for debugging. Next, the only thing is left is calling mMediaRecorder.start() I do this on a button click. In fact, I've added 2 buttons, 1st button calls this above method, the other button makes the start call. I'm sure this same code worked for me once, I don't know why isn't it working anymore.
I added a try catch around my mMediaRecorder.start() call with an IllegalStateException and here's what I got:
04-09 10:38:18.350: E/MediaRecorder(29678): start failed: -38
04-09 10:38:18.350: E/VideoTut_Main(29678): Illegal state exception with MediaRecorder.Start() : null
I have written a lengthy article in my blog about problems like this. Summarizing it there are (at least to my knowledge) three methods to set up the Android mediarecorder and for each method I have had a device in my hands where this method did not work. So you need the user to try out which method works on his device. The methods are as follows:
getSupportedVideoSizes()
from the camera parameters.Method 1. and 3. require setting the output format, audio encoder and video encoder explicitly for which I always use
MediaRecorder.OutputFormat.THREE_GPP
,MediaRecorder.AudioEncoder.AMR_NB
andMediaRecorder.VideoEncoder.MPEG_4_SP
.I have a small free app called Video Timer in Google Play which has implemented all three methods so you can try out easily what works on the Nexus 5.