MediaCodec 配置方法不会永久崩溃

Not permanent crash of MediaCodec configure method

当我尝试以下一种方式配置 MediaCodec 时,我有时会遇到 android.media.MediaCodec$CodecException: Error 0x80001001:

MediaFormat outputFormat = new MediaFormat();
outputFormat.setString(MediaFormat.KEY_MIME, COMPRESSED_AUDIO_FILE_MIME_TYPE);
outputFormat.setInteger(MediaFormat.KEY_CHANNEL_COUNT, 2);
outputFormat.setInteger(MediaFormat.KEY_SAMPLE_RATE, frequency);
outputFormat.setInteger(MediaFormat.KEY_AAC_PROFILE, MediaCodecInfo.CodecProfileLevel.AACObjectLC);
outputFormat.setInteger(MediaFormat.KEY_BIT_RATE, COMPRESSED_AUDIO_FILE_BIT_RATE);
outputFormat.setInteger(MediaFormat.KEY_MAX_INPUT_SIZE, 0);

MediaCodec codec = MediaCodec.createEncoderByType(COMPRESSED_AUDIO_FILE_MIME_TYPE);
codec.configure(outputFormat, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
codec.start();

完整的崩溃日志:

E/OMXMaster﹕ A component of name 'OMX.qcom.audio.decoder.aac' already exists, ignoring this one.
E/ACodec﹕ [OMX.google.aac.encoder] configureCodec returning error -38
E/ACodec﹕ signalError(omxError 0x80001001, internalError -2147483648)
E/MediaCodec﹕ Codec reported err 0x80001001, actionCode 0, while in state 3
E/MediaCodec﹕ configure failed with err 0x80001001, resetting...
I/OMXClient﹕ Using client-side OMX mux.
E/OMXMaster﹕ A component of name 'OMX.qcom.audio.decoder.aac' already exists, ignoring this one.

所以据我了解,问题出现在本机代码中。但它只出现在某些设备上 - 例如在 Android 5.1 的 Nexus 7 上,而具有相同 android 版本的 Nexus 10 完美运行。

看来我找到了MediaCodec的来源和崩溃的地方。这里的 link http://sourceforge.net/p/opencore-amr/vo-aacenc/ci/2418ead75aa9cdaf01cb4286f38fb7be2d48bd8d/tree/aacenc/SoftAACEncoder2.cpp#l362.

实际上我的音频文件的 WAV header 有问题。我在asynctask中运行一个converting,但是在它的运行后面写了一个header。所以,有时我试图在没有 header 的情况下转换 wav 文件。因为我从 wav header 中提取了一些参数(如频率等),所以我得到了 0 值。结果,MediaCodec 无法配置并崩溃。