Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/204.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android:无法实例化MediaCodec,因为配置时出现IllegalArgumentException_Android_Android Mediacodec - Fatal编程技术网

Android:无法实例化MediaCodec,因为配置时出现IllegalArgumentException

Android:无法实例化MediaCodec,因为配置时出现IllegalArgumentException,android,android-mediacodec,Android,Android Mediacodec,我正在学习如何使用MediaCodec,因此我正在做一个实验来了解它是如何工作的,现在我正在尝试创建一个编解码器,它将从设备的摄像头接收数据,并将视频转换为不同的分辨率/比特率 我正在尝试使用曲面设置一个MediaCodec实例,但配置失败。当我点击配置代码行时,我在Logcat中看到以下错误: 2020-01-03 11:22:01.769 25735-25735/com.oscar.vtech.multistreamtest E/libc: Access denied finding pro

我正在学习如何使用
MediaCodec
,因此我正在做一个实验来了解它是如何工作的,现在我正在尝试创建一个编解码器,它将从设备的摄像头接收数据,并将视频转换为不同的分辨率/比特率

我正在尝试使用
曲面
设置一个
MediaCodec
实例,但配置失败。当我点击配置代码行时,我在
Logcat
中看到以下错误:

2020-01-03 11:22:01.769 25735-25735/com.oscar.vtech.multistreamtest E/libc: Access denied finding property "ro.kirin.product.platform"
2020-01-03 11:22:01.769 25735-25735/com.oscar.vtech.multistreamtest E/HwExtendedCodec: mime: video/avc matching compontent failed!
2020-01-03 11:22:01.782 25735-25918/com.oscar.vtech.multistreamtest E/ACodec: [OMX.hisi.video.encoder.avc] failed to set output port definition parameters.
2020-01-03 11:22:01.782 25735-25918/com.oscar.vtech.multistreamtest E/ACodec: [OMX.hisi.video.encoder.avc] configureCodec returning error -22
2020-01-03 11:22:01.782 25735-25918/com.oscar.vtech.multistreamtest E/ACodec: signalError(omxError 0x80001001, internalError -22)
2020-01-03 11:22:01.782 25735-25917/com.oscar.vtech.multistreamtest E/MediaCodec: Codec reported err 0xffffffea, actionCode 0, while in state 3
2020-01-03 11:22:01.785 25735-25735/com.oscar.vtech.multistreamtest E/MediaCodec: configure failed with err 0xffffffea, resetting...
2020-01-03 11:22:01.802 25735-25735/com.oscar.vtech.multistreamtest E/MainActivity: Unable to start recording: java.lang.IllegalArgumentException
实例化MediaCodec并对其进行配置的代码如下:

public class VideoEncoder {
    private static final String TAG = VideoEncoder.class.getSimpleName();
    private static final String DEFAULT_MIME_TYPE = MediaFormat.MIMETYPE_VIDEO_AVC;                             // H.264 Advanced Video Coding
    private static final int DEFAULT_COLOR_FORMAT = MediaCodecInfo.CodecCapabilities.COLOR_FormatYUV420Planar;
    private static final int DEFAULT_FRAME_RATE = 30;
    private static final int DEFAULT_FRAME_INTERVAL = 3;

    private MediaCodec mediaCodec;
    private Surface inputSurface;

    public VideoEncoder(int width, int height, int bitrate) throws IOException {
        MediaFormat mediaFormat = MediaFormat.createVideoFormat(DEFAULT_MIME_TYPE, width, height);

        mediaFormat.setInteger(MediaFormat.KEY_COLOR_FORMAT, DEFAULT_COLOR_FORMAT);
        mediaFormat.setInteger(MediaFormat.KEY_BIT_RATE, bitrate);
        mediaFormat.setInteger(MediaFormat.KEY_FRAME_RATE, DEFAULT_FRAME_RATE);
        mediaFormat.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, DEFAULT_FRAME_INTERVAL);

        Log.d(TAG, String.format("mediaFormat: %s", mediaFormat));

        this.mediaCodec = MediaCodec.createEncoderByType(DEFAULT_MIME_TYPE);

        this.mediaCodec.configure(mediaFormat, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);

        this.inputSurface = this.mediaCodec.createInputSurface();

        this.mediaCodec.start();
    }

    public Surface getInputSurface() {
        return inputSurface;
    }
}
这是设置的
mediaFormat
值的调试日志:

color-format=19, i-frame-interval=3, mime=video/avc, width=842, bitrate=2000, frame-rate=30, height=480

如果有什么地方我可以学习更多关于如何使用编解码器的知识,这样我就可以理解如何正确使用
MediaCodec
,请分享,我会非常感激。

您可能会有更多的运气尝试
颜色格式化v420半平面
,并使用更高的比特率,对于初学者,可以尝试10万左右的东西,直到你能让它工作为止。有关您需要阅读的一些文档,请参阅。@greeble31我尝试过使用不同的颜色格式,如
color\u formatyuv420 flexible
,并使用不同的比特率,如
2000*1000
,但编解码器仍然无法配置,或者无法配置或创建曲面。我是否应该担心此错误消息<代码>E/HwExtendedCodec:mime:video/avc匹配组件失败是的,你应该。这可能对应于无效的配置。但是,同样地,
非法状态异常的存在也是如此。现在,您可以尝试任何您喜欢的
COLOR\u格式
,但是如果它们在
MediaCodecInfo
中没有表示,请不要期望函数成功。您好,您可以启动此测试吗?或者使用MediaCodecInfo.VideoCapabilities来显示您的设备功能?