1

所以我意识到 MediaRecorder 启动错误有很多解决方案,但是其中大多数都围绕“启动失败:-19”,它已链接到“NO_INIT”请参阅此处选择答案中的评论。我还没有找到任何解决方案,甚至没有找到-16 的解释。

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

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:发现错误代码 -16 实际上是 -EBUSY。在扩展 LogCat 以包含所有内容并彻底梳理 Android 源代码后,我发现它与无法调整(初始化)相机设置有关,我不确定我做错了什么,但它是一个开始。如果您可以访问 AOSP,您可以看到错误来自 /frameworks/av/media/libstagefright/CameraSource.cpp CameraSource::ConfigureCamera

4

1 回答 1

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;
}  
于 2013-01-17T03:28:24.163 回答