配置 MediaCodec 时出错 "Failed to set standard component role 'audio_encoder.aac'"

Error "Failed to set standard component role 'audio_encoder.aac'" when configuring a MediaCodec

以下代码用于配置 AAC 编码器:

mfAACEncoder = new MediaFormat();
mfAACEncoder.setString(MediaFormat.KEY_MIME, "audio/mp4a-latm");
mfAACEncoder.setInteger(MediaFormat.KEY_CHANNEL_COUNT, 1);
mfAACEncoder.setInteger(MediaFormat.KEY_SAMPLE_RATE, iClockRateAudio);
mfAACEncoder.setInteger(MediaFormat.KEY_BIT_RATE, 64 * 1024);//AAC-HE 64kbps
mfAACEncoder.setInteger(MediaFormat.KEY_AAC_PROFILE, MediaCodecInfo.CodecProfileLevel.AACObjectLC);

//
mfAACEncoder.setInteger(MediaFormat.KEY_MAX_INPUT_SIZE, 0);

mcAACEncoder = MediaCodec.createDecoderByType("audio/mp4a-latm");
mcAACEncoder.configure(mfAACEncoder, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);

mcAACEncoder.configure() 在 logcat 中生成以下内容:

06-22 11:13:09.919 4203-4666/net.mydomain.myapp I/MediaCodec: Found 0 pieces of codec specific data.
06-22 11:13:09.919 4203-4666/net.mydomain.myapp D/ACodec: onConfigureComponent
06-22 11:13:09.919 4203-4666/net.mydomain.myapp E/OMXNodeInstance: setParameter(1:google.aac.decoder, ParamStandardComponentRole(0x1000017)) ERROR: Undefined(0x80001001)
06-22 11:13:09.920 4203-4666/net.mydomain.myapp W/ACodec: [OMX.google.aac.decoder] Failed to set standard component role 'audio_encoder.aac'.
06-22 11:13:09.920 4203-4666/net.mydomain.myapp E/ACodec: setComponentRole err 80000000
06-22 11:13:09.920 4203-4666/net.mydomain.myapp E/ACodec: [OMX.google.aac.decoder] configureCodec returning error 80000000
06-22 11:13:09.920 4203-4666/net.mydomain.myapp E/ACodec: signalError(omxError 0x80001001, internalError -2147483648)
06-22 11:13:09.920 4203-4666/net.mydomain.myapp E/MediaCodec: Codec reported err 0x80001001, actionCode 0, while in state 3
06-22 11:13:09.933 4203-4632/net.mydomain.myapp D/ACodec: [OMX.MTK.VIDEO.DECODER.AVC] onOutputBufferDrained ID 0xb4031220
06-22 11:13:09.933 4203-4632/net.mydomain.myapp D/ACodec: queue NativeWindow
06-22 11:13:09.941 4203-4665/net.mydomain.myapp E/MediaCodec: configure failed with err 0x80001001, resetting...

有人可以提供有关如何解决此问题的提示吗?

如代码所示,the solution of this thread已经实现。

您的问题出在这里:

mcAACEncoder = MediaCodec.createDecoderByType("audio/mp4a-latm");
mcAACEncoder.configure(mfAACEncoder, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);

您想将其用作编码器,但您实际上创建了解码器组件,而是尝试将其用作编码器角色。要修复它,请将第一行改为:

mcAACEncoder = MediaCodec.createEncoderByType("audio/mp4a-latm");