我尝试为“audio/3gpp”和我的应用程序崩溃创建编码器。
我配置 MediaCodec...
String mMime = "audio/3gpp";
mMediaCodec = MediaCodec.createEncoderByType(mMime);
MediaFormat mMediaFormat = MediaFormat.createAudioFormat(mMime, 44100, 1);
mMediaFormat.setInteger(MediaFormat.KEY_BIT_RATE, 12000);
mMediaFormat.setInteger(MediaFormat.KEY_SAMPLE_RATE, 44100);
mMediaFormat.setInteger(MediaFormat.KEY_CHANNEL_COUNT, 1);
mMediaCodec.configure(mMediaFormat, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
mMediaCodec.start();
然后我尝试对数据进行编码......
private byte[] EncodeDataTo3gp(byte[] input)
{
byte[] outData = null;
try
{
ByteBuffer[] inputBuffers = mMediaCodec.getInputBuffers();
ByteBuffer[] outputBuffers = mMediaCodec.getOutputBuffers();
int inputBufferIndex = mMediaCodec.dequeueInputBuffer(-1);
if (inputBufferIndex >= 0)
{
ByteBuffer inputBuffer = inputBuffers[inputBufferIndex];
inputBuffer.clear();
inputBuffer.put(input);
mMediaCodec.queueInputBuffer(inputBufferIndex, 0, input.length, 0, 0);
}
MediaCodec.BufferInfo bufferInfo = new MediaCodec.BufferInfo();
int outputBufferIndex = mMediaCodec.dequeueOutputBuffer(bufferInfo, 0);
while (outputBufferIndex >= 0)
{
ByteBuffer outputBuffer = outputBuffers[outputBufferIndex];
outData = new byte[bufferInfo.size];
outputBuffer.get(outData);
mMediaCodec.releaseOutputBuffer(outputBufferIndex, false);
outputBufferIndex = mMediaCodec.dequeueOutputBuffer(bufferInfo, 0);
}
} catch (Throwable t) {
t.printStackTrace();
}
return outData;
}
但是这个函数抛出了一个异常。什么时候"inputBuffer.put(input);": java.nio.BufferOverflowException