Android can't record video with Front Facing Camera, MediaRecorder start failed: -19

Madhava Jay picture Madhava Jay · Feb 4, 2013 · Viewed 25.4k times · Source

I have two different code bases with the same problem.

The first one is code copied straight from developer.android.com here: http://developer.android.com/guide/topics/media/camera.html#custom-camera

The second one is this code:

http://android-er.blogspot.com.au/2011/10/simple-exercise-of-video-capture-using.html

Both work fine with the normal rear camera, but as soon as I try to use the front facing camera I get the error.

This happens on the following devices:

  • Nexus S 4.1.2

  • Galaxy Nexus 4.1.2

  • Nexus 7 4.2.1 (it only has front facing camera)

I have tried what looks like 2.2 era Camera Params as well, which some people claim is required with some Samsung and HTC devices, although multiple different articles reference different String Keys:

c = Camera.open(frontFacingCameraID); // attempt to get a Camera instance
Camera.Parameters params = c.getParameters();
params.set("cam-mode", 1);
params.set("cam_mode", 1);
params.set("camera-id", 1);
c.setParameters(params);

None of these work, also please note that I am detecting the correct Front Facing Camera ID which on the Nexus 7 is of course: 0. But the results are the same on all the devices.

I have tried using low quality profile, I have tried setting the video resolution, encoder, output format, bitrate, frame rate and video size manually in a multitude of ways but none which have worked.

The thing which makes me think theres nothing wrong with most of the code is that the regular camera works fine. So my guess is its something to do with the prepareVideoRecorder() / prepareMediaRecorder() method which sets up the Media Recorder.

Perhaps a Media Recorder manual encoding settings that are known to work on a front facing camera?

I have to say, the Android Camera and MediaRecorder API's suck. Compared with iOS its a bit of a mess, not to mention some of the scary looking param incompatibility issues and different resolutions across the fragmented device landscape.

Assuming I can get it working on my JB devices, does anyone know from experience if most of these issues are resolved with API 15 ICS?

I would consider not supporting API 10 Gingerbread if its going to be too hard to support.

Answer

spitzanator picture spitzanator · Mar 4, 2013

I wrestled with this problem a bit today, too.

First, make sure that your permissions are set up correctly. Specifically, to record video, you'll want:

<uses-feature android:name="android.hardware.camera.front" />
<uses-feature android:name="android.hardware.microphone"/>

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Second, and this is the tricky part, this line from the tutorial does not work with the front-facing camera!

mMediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));

That signature for CamcorderProfile.get() defaults to a profile for the back-facing camera:

Returns the camcorder profile for the first back-facing camera on the device at the given quality level. If the device has no back-facing camera, this returns null.

Instead, use http://developer.android.com/reference/android/media/CamcorderProfile.html#get(int,%20int). The first parameter is the id of the camera that you opened, specifically, the front-facing camera.