android - 使用 MediaCodec 解码原始 AAC 音频数据适用于 Android 4.4 但不适用于 4.3

标签 android aac android-mediacodec

我已经使用 MediaCodec 成功地实现了对原始 AAC-LC 的编码/解码。我正在使用描述的相同技术 here对数据进行编码。但是,我存储原始 AAC 数据(没有 header ),然后在通过 MediaCodec 解码器传递数据时即时附加 header 。这一切在运行 Android 4.4 的 Nexus 4 和 Nexus 5 上绝对完美。但是,在 Galaxy Nexus(运行 Android 4.3)上我得到:

W/SoftAAC2(1234): AAC decoder returned error 16388, substituting silence

错误 16388 表示 decode frame error .

我尝试过使用和不使用初始 MediaCodec.BUFFER_FLAG_CODEC_CONFIG,但这没有什么区别。

这里是重现错误的最简单的情况(使用配置数据包):

MediaFormat format = new MediaFormat();
format.setInteger(MediaFormat.KEY_SAMPLE_RATE, 44100);
format.setInteger(MediaFormat.KEY_CHANNEL_COUNT, 1);
format.setString(MediaFormat.KEY_MIME, "audio/mp4a-latm");
format.setInteger(MediaFormat.KEY_AAC_PROFILE, MediaCodecInfo.CodecProfileLevel.AACObjectLC);
format.setInteger(MediaFormat.KEY_IS_ADTS, 1); 

byte[] bytes = new byte[]{(byte) 0x11, (byte)0x90};
ByteBuffer bb = ByteBuffer.wrap(bytes);
format.setByteBuffer("csd-0", bb);

MediaCodec codec = MediaCodec.createDecoderByType("audio/mp4a-latm");
codec.configure(format, null /* surface */, null /* crypto */, 0 /* flags */);
codec.start();
ByteBuffer[] codecInputBuffers = codec.getInputBuffers();

int inputBufIndex = codec.dequeueInputBuffer(TIMEOUT_US);
if (inputBufIndex >= 0) {
    ByteBuffer dstBuf = codecInputBuffers[inputBufIndex];
    byte[] data = {-1, -7, 80, 0, 1, 63, -4, 18, 8}; // values taken from SO answer linked above (chanCfg = 1, packetLen = 9)
    dstBuf.clear();
    dstBuf.put(data);
    codec.queueInputBuffer(inputBufIndex, 0, data.length, 0, MediaCodec.BUFFER_FLAG_CODEC_CONFIG);
}

显然,代码远不止于此,但这包括直到出现错误消息为止的所有已执行代码。

最佳答案

解决方案是包含 ADTS header 。 4.3 和 4.4 都支持没有 ADTS header 的数据包。

关于android - 使用 MediaCodec 解码原始 AAC 音频数据适用于 Android 4.4 但不适用于 4.3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26084136/

相关文章:

android - LeakCanary 没有提供足够的信息来识别泄漏

java - 单击按钮时,内容不会改变。 - 安卓

android - 如何更改 ListPreference 对话框的外观

ffmpeg - 在 macOS 上捕获相机 + 麦克风并编码为 h264/aac

ffmpeg - 从歌曲文件中提取未解码的音频有效载荷?

android - 如何在 Android 中录制视频时嵌入文本?

android - 使用 MediaCodec 创建 AAC 编码器时出错

android - Android中YouTube分享的视频是什么格式

ios - 从 RTMP 流 ios 播放 aac 音频

android - 如何在 Android 中使用 MediaCodec Api 通过 RTP 解码 H264