Android 无法使用前置摄像头录制视频,MediaRecorder 启动失败:-19 [英] Android can't record video with Front Facing Camera, MediaRecorder start failed: -19

查看:100
本文介绍了Android 无法使用前置摄像头录制视频,MediaRecorder 启动失败:-19的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个不同的代码库有同样的问题.

I have two different code bases with the same problem.

第一个是直接从 developer.android.com 复制的代码:http://developer.android.com/guide/topics/media/camera.html#custom-camera

第二个是这个代码:

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(只有前置摄像头)

我也尝试过类似 2.2 时代的相机参数,有些人声称某些三星和 HTC 设备需要它,尽管多篇不同的文章引用了不同的字符串键:

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);

这些都不起作用,另外请注意,我正在检测正确的前置摄像头 ID,在 Nexus 7 上当然是:0.但结果在所有设备上都是一样的.

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.

让我觉得大部分代码都没有问题的是普通相机工作正常.所以我的猜测是它与设置媒体记录器的 prepareVideoRecorder()/prepareMediaRecorder() 方法有关.

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?

我不得不说,Android 相机和 MediaRecorder API 很烂.与 iOS 相比,它有点混乱,更不用说一些看起来很可怕的参数不兼容问题以及分散的设备环境中的不同分辨率.

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.

假设我可以让它在我的 JB 设备上运行,有没有人根据经验知道这些问题中的大多数是否可以通过 API 15 ICS 解决?

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?

如果 API 10 Gingerbread 太难支持,我会考虑不支持.

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

推荐答案

好的,我终于可以正常工作了.

这个问题似乎肯定与Profile Settings有关,尤其是Frame Rate.

The issue seems to definitely relate to Profile Settings and in particular Frame Rate.

在我的主要测试设备 Nexus S 上,如果我探测相机,我会收到以下参数:

On the Nexus S, my primary test device, if I probe the Camera I receive following Parameters:

对于后置摄像头:
15 FPS 到 30 FPS,足够公平.

For the Rear Camera:
15 FPS to 30 FPS, fair enough.

对于前置摄像头:
7.5 FPS 到 30 FPS,可以.

For the Front Facing Camera:
7.5 FPS to 30 FPS, okay.

然后我检查我尝试使用的配置文件:

Then I check the Profiles I am trying to use:

CamcorderProfile.QUALITY_HIGH
CamcorderProfile.QUALITY_LOW

质量_低:

音频比特率:12200
音频频道:1
音频编解码器:AMR_NB音频采样率:8000
持续时间:30
文件格式:THREE_GPP
质量:0
视频比特率:256000
视频编解码器:H264
视频帧率:30
视频帧宽度:176
视频帧高度:144

audioBitRate: 12200
audioChannels: 1
audioCodec: AMR_NB audioSampleRate: 8000
duration: 30
fileFormat: THREE_GPP
quality: 0
videoBitRate: 256000
videoCodec: H264
videoFrameRate: 30
videoFrameWidth: 176
videoFrameHeight: 144

质量_高:

音频比特率:24000
音频频道:1
音频编解码器:AAC
音频采样率:16000
持续时间:60
文件格式:MPEG_4
质量:1
视频比特率:3000000
视频编解码器:H264
视频帧率:30
视频帧宽度:720
视频帧高度:480

audioBitRate: 24000
audioChannels: 1
audioCodec: AAC
audioSampleRate: 16000
duration: 60
fileFormat: MPEG_4
quality: 1
videoBitRate: 3000000
videoCodec: H264
videoFrameRate: 30
videoFrameWidth: 720
videoFrameHeight: 480

显然,高质量配置文件适用于后置摄像头,因为正面只有 640x480.但他们都说 30 FPS.

Clearly, the High Quality Profile is meant for the Rear Camera, seeing as the front facing is only 640x480. But they both state 30 FPS.

现在……奇怪的是:

如果我为后置摄像头设置了任何帧速率,无论是什么配置文件,它都会崩溃:

If I set ANY frame rate for the rear facing camera, no matter what profile, it crashes with the dreaded:

-19 错误

mediaRecorder.setVideoFrameRate(fpsInt);

这没什么大不了的,因为我不关心后置摄像头,但这很奇怪,考虑到配置文件默认为 30,并且 Params 说他们接受 15-30.但是我尝试过的 int 值都没有奏效.如果我省略 setVideoFrameRate 就好了.

That's not a big deal coz I don't care about the rear camera but it is weird, considering the profiles are defaulting to 30 and the Params say they accept 15-30. But no int value I've tried has worked. If I omit the setVideoFrameRate it's fine.

无论如何,转到前置摄像头.

因此,如果我使用 QUALITY_LOW 配置文件并将帧速率设置为 15 或更低,它会神奇地工作.

So, if I use the QUALITY_LOW profile AND set the frame-rate to 15 or lower, it magically works.

mediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_LOW));
mediaRecorder.setVideoFrameRate(15);

实际上任何值,1 - 15 都有效.这看起来很奇怪.

In fact any value, 1 - 15 works. Which seems weird.

所以这是一个难题,我可能可以探索分辨率并为大多数相机选择合适的分辨率,尽管我也相当有信心几乎所有前置摄像头的最低 VGA 为 640x480.

So here's the conundrum, I can probably probe for resolution and select an appropriate res for most cameras, although I'm also fairly confident almost all front-facing cameras at minimum VGA 640x480.

但是,帧率呢?在 Nexus S 的情况下,我看不出有任何方法可以确定 15 或更低的值而不只是猜测?我是否应该始终使用相机返回的最低帧率?

But, what about the frame rate? In the case of the Nexus S, I don't see any way I could determine the value of 15 or lower without just guessing? Should I aim to always use the LOWEST frame-rate that is returned by the Camera?

我看了一下 Galaxy Nexus,它有 3 个帧速率范围,第一个是 15 - 15,第二个是 15 - 30.它的低质量配置文件相似,但分辨率更高.如果我在 Galaxy Nexus 上使用低调,它似乎工作正常.

I took a look at the Galaxy Nexus and it has 3 frame rate ranges, the first one is 15 - 15 and the second is 15 - 30. Its low quality profile is similar albeit higher resolution. If I use low profile on Galaxy Nexus it seems to work fine.

使用 Nexus 7,我无法探测 CamcorderProfile 我一直得到空指针,这很奇怪.它说它支持 4 FPS - 60 FPS.如果我选择 QUALITY_LOW 你认为它应该可以工作,它会崩溃,而且我找不到它可以使用的帧速率.虽然错误与 setProfile 相关,但我认为问题在于内置配置文件.Android API 的重点当然是它的一致性,这是一个旗舰设备,FF 摄像头是用于视频会议的,不是吗???????

With the Nexus 7, I cant probe the CamcorderProfile's I keep getting null pointers, which is weird. It says it supports 4 FPS - 60 FPS. If I choose QUALITY_LOW which you'd think it should work, it crashes, and I can't find a frame rate it will work with. Although the error relates to setProfile, so I think the issue is with the built in profile. Surely the point of Android API is that it's consistent, this is a flag ship device and the FF camera is there for Video Conferencing isn't it?????

因此,虽然我让它在两台设备上使用手动自定义设置工作,但我看不到通过代码使其在多个设备上工作的明确方法.

So, while I have it working on two of the devices using manual custom settings for each, I can't see a clear way of making it work across multiple devices through code.

似乎 Nexus S 并未按照其承诺的方式按照其 Camera.getParameters().getSupportedPreviewFpsRange()

It seems that the Nexus S does not behave the way it promises to with regards to setting the FPS as per its Camera.getParameters().getSupportedPreviewFpsRange()

我很高兴它使用自动 FPS 设置,但显然它不会与 FF 相机一起使用,那我该怎么办?我必须在 Nexus S 上明确设置 FPS,在本例中设置为 1 到 15 FPS,尽管相机告诉我它可以处理 7.5 - 30 FPS.

I'm all happy for it to use Auto FPS settings but apparently it won't with the FF camera so what am I supposed to do? I have to explicitly set the FPS on the Nexus S and in this case to anything from 1 to 15 FPS, despite the Camera telling me it handles 7.5 - 30 FPS.

似乎 setProfile 承诺解决 2.x 中的所有问题并不完全正确.

Seems like the promise of the setProfile fixing all the issues in 2.x wasn't entirely true.

我可以理解,如果您为特定 ROM 编写相机应用程序,您只是将其自定义为该特定硬件,这可能解释了为什么人们似乎总是在自定义 ROM 上安装有缺陷的相机应用程序.但是..... 可下载的视频录制应用程序如何工作?它们是针对每台设备定制的吗?

I can understand if your writing the Camera App for a particular ROM you just customize it to that particular hardware, which might explain why people seem to always have buggy camera apps on custom ROMs. BUT..... How do downloadable video recording apps work? Are they custom to each device?

这就是 Android 上还没有 Facebook Poke 和 Twitter Vine 的原因吗????:P

Is this why there's no Facebook Poke and Twitter Vine on Android yet???? :P

Google,您的相机 API 是什么?

Google, what is with your Camera API?

有人知道确定所有 API 15+ 兼容设备的分辨率和帧速率的最佳实践"吗?

Does anyone know the "best practices" to determine resolution and frame-rate for all API 15+ compatible devices?

这是可能的,还是我要在我测试的每台设备上编写自定义代码,然后在其余设备上掷骰子?

Is that even possible, or am I going to be writing custom code on each device I test and then just roll the dice on the rest?

或者 Nexus S 和 Nexus 7 只是意外事故?

Or is the Nexus S and the Nexus 7 just freak accidents?

这篇关于Android 无法使用前置摄像头录制视频,MediaRecorder 启动失败:-19的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆