1. 程式人生 > 其它 >MediaCodec.configure()報錯 [OMX.MTK.VIDEO.ENCODER.AVC] configureCodec returning error -38

MediaCodec.configure()報錯 [OMX.MTK.VIDEO.ENCODER.AVC] configureCodec returning error -38

技術標籤:Android 音視訊android

Android 11 初始化MediaCodec出錯,
報錯資訊如下:

2020-12-28 17:31:26.783 15163-15163/com.android.wcamera D/MPEG4Writer: PreAllocation disabled. fallocate : Operation not supported on transport endpoint, 95
2020-12-28 17:31:26.783 15163-15163/com.android.wcamera E/BaseMediaEncoder: start initVideoEncodec!
2020-12-28 17:31:26.785 15163-15255/com.android.wcamera I/OMXClient: IOmx service obtained 2020-12-28 17:31:26.789 15163-15255/com.android.wcamera E/ACodec: [OMX.MTK.VIDEO.ENCODER.AVC] configureCodec returning error -38 2020-12-28 17:31:26.789 15163-15255/com.android.wcamera E/ACodec: signalError(omxError 0x80001001
, internalError -2147483648) 2020-12-28 17:31:26.789 15163-15254/com.android.wcamera E/MediaCodec: Codec reported err 0x80001001, actionCode 0, while in state 3 2020-12-28 17:31:26.790 15163-15163/com.android.wcamera E/MediaCodec: configure failed with err 0x80001001, resetting... 2020-12-28 17:31:26.793 15163-15255/com.android.
wcamera I/OMXClient: IOmx service obtained 2020-12-28 17:31:26.796 15163-15163/com.android.wcamera E/BaseMediaEncoder: initVideoEncodec exception:android.media.MediaCodec$CodecException: Error 0x80001001 2020-12-28 17:31:26.924 15163-15233/com.android.wcamera I/BufferQueueProducer: [SurfaceTexture-3-15163-0](id:3b3b00000000,api:4,p:779,c:15163) connect(): api=4 producerControlledByApp=true

初始化MediaCodec 程式碼如下:

            LogUtil.e(TAG, "start initVideoEncodec!");
            videoBufferInfo = new MediaCodec.BufferInfo();
            videoFormat = MediaFormat.createVideoFormat(mimeType, width, height);
            videoFormat.setInteger(MediaFormat.KEY_COLOR_FORMAT, MediaCodecInfo.CodecCapabilities.COLOR_FormatSurface);
            videoFormat.setInteger(MediaFormat.KEY_BIT_RATE, width * height * 4);
            videoFormat.setInteger(MediaFormat.KEY_FRAME_RATE, 30);
            videoFormat.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, 1);
            videoFormat.setInteger(MediaFormat.KEY_BITRATE_MODE,
                    MediaCodecInfo.EncoderCapabilities.BITRATE_MODE_CQ);
            videoFormat.setInteger("level", MediaCodecInfo.CodecProfileLevel.AVCProfileHigh);
            videoEncodec = MediaCodec.createEncoderByType(mimeType);
            videoEncodec.configure(videoFormat, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);

刪除

            videoFormat.setInteger(MediaFormat.KEY_BITRATE_MODE,
                    MediaCodecInfo.EncoderCapabilities.BITRATE_MODE_CQ);

即可