首页 > 解决方案 > Android:由于配置时的 IllegalArgumentException,无法实例化 MediaCodec

问题描述

我正在学习如何使用,MediaCodec所以我正在做一个实验来了解它是如何工作的,现在我正在尝试创建一个编解码器,它将从设备的相机接收数据并将素材转码为不同的分辨率/比特率。

我正在尝试使用 设置一个MediaCodec实例Surface,但我未能对其进行配置。这些是我在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,请分享,我将非常感激。

标签: androidandroid-mediacodec

解决方案


推荐阅读