我需要将音频文件从PCM转换为AMR,我尝试使用Android API 16+中的MediaCodec。我在调用它的queueInputBuffer时遇到了IllegalStateException:
07-16 19:40:55.519: E/AndroidRuntime(19227): FATAL EXCEPTION: main
07-16 19:40:55.519: E/AndroidRuntime(19227): java.lang.IllegalStateException
07-16 19:40:55.519: E/AndroidRuntime(19227): at android.media.MediaCodec.queueInputBuffer(Native Method)
这是我的代码:
private static MediaCodec mediaCodec;
private static void initMediaCodec() {
mediaCodec = MediaCodec.createEncoderByType("audio/3gpp");
MediaFormat mediaFormat = MediaFormat.createAudioFormat("audio/3gpp", 8000, 1);
mediaFormat.setInteger(MediaFormat.KEY_BIT_RATE, 12800);
mediaFormat.setInteger(MediaFormat.KEY_SAMPLE_RATE, 8000);
mediaFormat.setInteger(MediaFormat.KEY_CHANNEL_COUNT, 1);
mediaCodec.configure(mediaFormat, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
mediaCodec.start();
}
<...>
initMediaCodec();
ByteBuffer[] inputBuffers = mediaCodec.getInputBuffers();
ByteBuffer[] outputBuffers = mediaCodec.getOutputBuffers();
int inputBufferIndex = mediaCodec.dequeueInputBuffer(-1);
if (inputBufferIndex >= 0) {
ByteBuffer inputBuffer = inputBuffers[inputBufferIndex];
inputBuffer.clear();
inputBuffer.put(fileBuffer); //fileBuffer is ByteBuffer from my input audio file
mediaCodec.queueInputBuffer(inputBufferIndex, 0, fileBuffer.capacity(), 0, 0);
}
<...>
我提到inputBuffers有4个元素,容量为320,我的音频文件缓冲区容量可能是96000 +。
任何帮助都将不胜感激。谢谢!