4

So I have been working on a recording module for a larger application. It's fully functional on most devices, but there is one batch of phones that simply refuse recording.

Whenever I start the recording using the MediaRecorder, LogCat will graciously tell me that MediaRecorder failed to start, resulting in error code -12. I have been scouring the internets for most of the day looking for an explanation to these error codes. So far I have been unlucky. Hence I turn myself to the rest of the developer community.

MediaRecorder start failed -12

More than one question has already been asked about those error codes and more specifically 16, 19, 22 and one ridiculously large number 2147483648, but this information has proven to be useless in my case.

Does anyone know where I can find a reliable list of MediaRecorder error codes including the meaning of these? Or even better, explain to me what the almighty Android oracle is trying to signal me?

Thanks in advance

Daft Fox
  • 133
  • 10
  • Just a quick comment: 2147483648 is 2^31. Don't know why the API designers would use this maximum value. It is usually part of the range of signed ints. – darrenp Feb 05 '13 at 18:03

3 Answers3

4

My previous answer was not complete. After more tinkering I found out that many devices can be very sensitive to settings you would think of being default for most devices, but in fact there are many devices that do not behave the way you would expect and they will crash giving you many different error codes that you will not be able to find on the internet. These error codes range from -12, -19, -21 to -100 etc. and most of these error codes can be resolved by using the tips below. I will give a short list of findings below:

The HTC ChaCha running API 10 does not record using the H264 encoder. I got it to work using MP4 and/or H263, however the video still came out totally distorted. The solution to that was setting the camera preview size to 640*480 instead of using google's own "optimal preview" method which returns 576*432. By setting exceptions for this model, I was able to make it record perfectly.

The Galaxy Y running API 10 crashes when trying to record using H264. Made an exception for this one to use MP4 encoder. Fixed it.

The Galaxy Ace running API 10 crashes when trying to record using MP4 returning error code -12. Adding an exception using the H264 encoder for this device model fixes the problem.

The Desire Z running API 10 crashes when trying to record using MP4 encoder. Gave me an error -100 (Media server died). Using H264 fixes the problem.

if(API >=11){
        if(CamcorderProfile.hasProfile(mCamId, CamcorderProfile.QUALITY_480P)) {
            Log.d(TAG, "API 11+ 480P");
            mMediaRecorder.setProfile(CamcorderProfile.get(mCamId, CamcorderProfile.QUALITY_480P));
            mMediaRecorder.setVideoSize(640, 480);
            mMediaRecorder.setVideoEncodingBitRate(2000000);
        } else {
            Log.d(TAG, "API 11+ LOW");
            mMediaRecorder.setProfile(CamcorderProfile.get(mCamId, CamcorderProfile.QUALITY_LOW));
        }
    } else if (API < 11 && !MODEL.equals("GT-S5830") && !MODEL.equals("vision")){
        mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        Log.d(TAG, "API 9 device, defaulting");
        mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
        mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
        mMediaRecorder.setVideoSize(640, 480);
        mMediaRecorder.setVideoEncodingBitRate(2000000);
    } else if ((API < 11) && MODEL.equals("GT-S5830") || MODEL.equals("vision")) {
        //Galaxy Ace and Desire Z
        mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
        mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
        mMediaRecorder.setVideoSize(640, 480);
        mMediaRecorder.setVideoEncodingBitRate(2000000);
    }

I bet I will run into more models being a metaphorical pain in the crack and when I do, I will add them to the list. To sum up; if you run into these annoying, almost unexplainable error codes, have a look at the MediaRecorder parameters. I advice against using the CamcorderProfile class for any API below 11, since many of these models crash while using a high quality instance of this. If your recorder video file shows weird green colours, semi-diagonal lines running through your screen, etc, try to set the preview size to match the recorder video size.

Camera.Parameters param = mCamera.getParameters();
param.setPreviewSize(width, height);
mCamera.setParameters( param );
user229044
  • 232,980
  • 40
  • 330
  • 338
Daft Fox
  • 133
  • 10
  • android is just very buggy - https://stackoverflow.com/questions/51332386/mediarecorder-and-videosource-surface-stop-failed-1007-a-serious-android-bug I hope MediaCodec is much better than MediaRecorder but it's very difficult to use – user25 Jul 14 '18 at 00:11
1

I have found the fix to the problem I was having. Turns out not all devices support high quality recording. I assumed it was relative, meaning that high quality was the highest possible quality for that device instead of a constant.

So I replaced:

`mMediaRecorder.setProfile(CamcorderProfile.get(camId, CamcorderProfile.QUALITY_HIGH));`

with:

`mMediaRecorder.setProfile(CamcorderProfile.get(camId, CamcorderProfile.QUALITY_LOW));`

and that fixed the problem. Only bug left now is how to define the highest quality a device supports without making it crash on runtime.

Daft Fox
  • 133
  • 10
  • On some device you have to set a camera parameter : "cam_mode"=1. That's for most Samsung devices and some htc, afaik. – Fildor Aug 09 '12 at 09:38
  • hmm have same problem and that dosen't work for me. What previe size you using? Maybe that's related somehow? – spirytus Aug 12 '12 at 22:54
  • I have replaced low quality with high (exactly an opposite), and that worked for me )lol – Nikita Dec 12 '12 at 12:46
0

This code worked for me :)

        camId = Main.mHelper.getFrontFacingCameraId();
        mCamera = Camera.open(camId);

        Camera.Parameters param = mCamera.getParameters();
        param.set( "cam_mode", 1 );     
        mCamera.setParameters(param);
Daniel
  • 753
  • 9
  • 15