MediaRecorder启动失败:-16(MediaRecorder start failed:

2019-10-17 00:13发布

所以,我知道有很多解决方案,在那里为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)

更新12年7月11日下午2点21分 :Discoverd的错误代码-16实际上是-EBUSY。 扩大logcat的,包括一切,彻底梳理的Android源代码后,我发现,它关系到一个inablility调整(初始化)的相机设置,我不知道我在做什么错,但它的一个开端。 如果你有机会获得AOSP你可以看到错误来自/frameworks/av/media/libstagefright/CameraSource.cpp CameraSource :: ConfigureCamera

Answer 1:

我跑这个错误(-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示例代码是从机器人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;
}  


文章来源: MediaRecorder start failed: -16