MediaRecorder启动失败:-16

时间:2012-07-11 14:32:43

标签: android mediarecorder android-mediarecorder

所以我意识到MediaRecorder启动错误有很多解决方案,但是大多数解决方案都围绕“启动失败:-19”,它已经链接到“NO_INIT”See comments in selected answer here。我没有找到任何解决方案,甚至没有找到-16的解释。

如果有人知道我在哪里可以找到这些错误代码的名称,或者有代码解决方案-16请在这里给出答案!!这是我正在使用的代码。 (HTC Thunderbolt,股票)

mr.setVideoSource(MediaRecorder.VideoSource.CAMERA); //mr is my mediaRecorder
mr.setAudioSource(MediaRecorder.AudioSource.MIC);
mr.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
mr.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
mr.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
mr.setOutputFile(ParcelFileDescriptor.fromSocket(soc).getFileDescriptor());  //soc is a java.net.ServerSocket pointing to a port on the device

mr.setVideoSize(480,800);
mr.setVideoFrameRate(5);
mr.prepare();
mr.start(); //Crashes with "start failed: -16"

logcat的

07-11 10:14:34.879: V/MediaRecorderJNI(17939): JNIMediaRecorderListener::setCamera
07-11 10:14:34.879: V/MediaRecorderJNI(17939): process_media_recorder_call
07-11 10:14:47.549: V/MediaRecorderJNI(17939): setVideoSource(1)
07-11 10:14:47.549: V/MediaRecorderJNI(17939): getMediaRecorder E
07-11 10:14:47.559: V/MediaRecorderJNI(17939): process_media_recorder_call
07-11 10:14:47.559: V/MediaRecorderJNI(17939): setAudioSource(1)
07-11 10:14:47.559: V/MediaRecorderJNI(17939): getMediaRecorder E
07-11 10:14:47.559: V/MediaRecorderJNI(17939): process_media_recorder_call
07-11 10:14:47.559: V/MediaRecorderJNI(17939): setOutputFormat(0)
07-11 10:14:47.559: V/MediaRecorderJNI(17939): getMediaRecorder E
07-11 10:14:47.559: V/MediaRecorderJNI(17939): process_media_recorder_call
07-11 10:14:47.559: V/MediaRecorderJNI(17939): setVideoEncoder(0)
07-11 10:14:47.559: V/MediaRecorderJNI(17939): getMediaRecorder E
07-11 10:14:47.559: V/MediaRecorderJNI(17939): process_media_recorder_call
07-11 10:14:47.559: V/MediaRecorderJNI(17939): setAudioEncoder(0)
07-11 10:14:47.559: V/MediaRecorderJNI(17939): getMediaRecorder E
07-11 10:14:47.559: V/MediaRecorderJNI(17939): process_media_recorder_call
07-11 10:14:47.559: V/MediaRecorderJNI(17939): setVideoFrameRate(5)
07-11 10:14:47.559: V/MediaRecorderJNI(17939): getMediaRecorder E
07-11 10:14:47.559: V/MediaRecorderJNI(17939): process_media_recorder_call
07-11 10:14:47.559: V/MediaRecorderJNI(17939): setOutputFile
07-11 10:14:47.559: V/MediaRecorderJNI(17939): getMediaRecorder E
07-11 10:14:47.559: V/MediaRecorderJNI(17939): process_media_recorder_call
07-11 10:14:47.559: V/MediaRecorderJNI(17939): prepare
07-11 10:14:47.559: V/MediaRecorderJNI(17939): getMediaRecorder E
07-11 10:14:47.559: V/MediaRecorderJNI(17939): process_media_recorder_call
07-11 10:14:47.559: V/MediaRecorderJNI(17939): start
07-11 10:14:47.559: V/MediaRecorderJNI(17939): getMediaRecorder E
07-11 10:14:47.639: E/MediaRecorder(17939): start failed: -16
07-11 10:14:47.639: V/MediaRecorderJNI(17939): process_media_recorder_call
07-11 10:14:59.290: W/dalvikvm(17939): threadid=10: thread exiting with uncaught exception (group=0x4001d5a0)

更新07/11/12 2:21 PM :发现错误代码-16实际上是-EBUSY。在扩展LogCat以包含所有内容并梳理完Android源代码之后,我发现它与调整(初始化)相机设置无关,我不确定我做错了什么,但它是一个开始。如果您有权访问AOSP,您可以看到错误来自/frameworks/av/media/libstagefright/CameraSource.cpp CameraSource :: ConfigureCamera

1 个答案:

答案 0 :(得分:2)

我遇到了这个错误(启动时为-16)并发现它是由使用不支持的分辨率引起的。

首先,您必须从支持的尺寸

获得最佳尺寸
Parameters params = camera.getParameters();
        List<Size> sizes = params.getSupportedPreviewSizes();
        optimalSize = getOptimalPreviewSize(sizes, width, height);
params.setPreviewSize(optimalSize.width, optimalSize.height);

然后确保将预览和视频设置为相同的大小(如果我的经验不同,预览将在视频记录开始时冻结):

mediaRecorder.setVideoSize(optimalSize.width, optimalSize.height);

(getOptimalPreviewSize的示例代码来自android sdk)

private Size getOptimalPreviewSize(List<Size> sizes, int w, int h) {
    final double ASPECT_TOLERANCE = 0.1;
    double targetRatio = (double) w / h;
    if (sizes == null) return null;

    Size optimalSize = null;
    double minDiff = Double.MAX_VALUE;

    int targetHeight = h;

    // Try to find an size match aspect ratio and size
    for (Size size : sizes) {
        double ratio = (double) size.width / size.height;
        if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) continue;
        if (Math.abs(size.height - targetHeight) < minDiff) {
            optimalSize = size;
            minDiff = Math.abs(size.height - targetHeight);
        }
    }

    // Cannot find the one match the aspect ratio, ignore the requirement
    if (optimalSize == null) {
        minDiff = Double.MAX_VALUE;
        for (Size size : sizes) {
            if (Math.abs(size.height - targetHeight) < minDiff) {
                optimalSize = size;
                minDiff = Math.abs(size.height - targetHeight);
            }
        }
    }
    return optimalSize;
}