Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
341 views
in Technique[技术] by (71.8m points)

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...