Question

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
Was it helpful?

Solution

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:

  1. Use the devices default settings for video size, preview size etc., but set video size explicitly in the media recorder to the size of the preview that you get from the camera parameters after you have started the preview window but before you unlock the camera for recording.
  2. Use camcorder profiles like you did in your code. It should work flawlessly on most devices that have an Android version 4.0 and above. I would recommend to set maximum file size and video length though, but doubt that this is the problem with the Nexus 5. And I know at least one other device (Xperia ray upgraded to latest firmware version) where this does not work as well.
  3. Use one of the explicit video sizes that you get from 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 and MediaRecorder.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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top