我按照官方Android文档使用MediaCodec对象为我的音频输入设置编码器。但是下面代码中的dequeueOutputBuffer方法总是返回-1。这个回报值意味着什么?
我的代码:
/*configuarion of MediaCodec object*/
codec = MediaCodec.createEncoderByType("audio/mp4a-latm");
MediaFormat format = new MediaFormat();
format.setString(MediaFormat.KEY_MIME, "audio/mp4a-latm");
format.setInteger(MediaFormat.KEY_CHANNEL_COUNT, 1);
format.setInteger(MediaFormat.KEY_SAMPLE_RATE, 44100);
format.setInteger(MediaFormat.KEY_BIT_RATE, 64 * 1024);
format.setInteger(MediaFormat.KEY_AAC_PROFILE,MediaCodecInfo.CodecProfileLevel.AACObjectHE);
codec.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
codec.start();
ByteBuffer[] inputBuffers = codec.getInputBuffers();
ByteBuffer[] outputBuffers = codec.getOutputBuffers();
/*main loop to encode audio data*/
for (;;) {
int inputBufferIndex = codec.dequeueInputBuffer(-1); -->this is never zero so data should be written correctly!
if (inputBufferIndex >= 0) {
// write data from audiorecord in queue
inputBuffer = inputBuffers[inputBufferIndex];
inputBuffer.clear();
inputBuffer.put(buf);-->data from audiorecord object
encoder.queueInputBuffer(inputBufferIndex, 0, buf.length, 0, 0);
codec.queueInputBuffer(inputBufferIndex, ...);
}
int outputBufferIndex = codec.dequeueOutputBuffer(bufferInfo, 0);-------->>> "-1"
if (outputBufferIndex >= 0) {
//do something but never called because outputbufferindex is always -1??
答案 0 :(得分:1)
当您通过非负timeoutUS
时,将-1作为返回值表示对dequeueOutputBuffer
的调用已超时。根据Android Sample,它返回以下整数
public static final int INFO_TRY_AGAIN_LATER = -1;
答案 1 :(得分:-1)
据我所知,你必须每次都在timestamp
增加"encoder.queueInputBuffer"
字段,因为如果每次都给出相同的时间戳,缓冲区可能会被encoder
删除。