gpt4 book ai didi

Android MediaCodec dequeueInputBuffer 总是返回 -1

转载 作者:太空宇宙 更新时间:2023-11-03 10:37:15 32 4
gpt4 key购买 nike

我正在尝试从 AudioRecord 对象中获取原始数据,并使用 MediaMuxer 和 MediaCodec 将其保存在一个文件中。

我启动编解码器,启动多路复用器,将数据加载到输入缓冲区,但没有这样的运气。

通过调试调查,我发现问题出在对 dequeueInputBuffer() 的调用中。看起来前几 block 数据成功了,但最终 dequeueInputBuffer() 只是不断地返回 -1

有什么明显的我遗漏的东西吗?这似乎是我正在填满输入缓冲区,但编解码器永远不会释放它们。

相关代码 fragment :

int numChunks = input.length / CHUNKSIZE;
mAudioEncoder.start();
for (int chunk = 0; chunk <= numChunks; chunk++) {
byte[] passMe = new byte[CHUNKSIZE];
int inputBufferIndex = -1;
Log.d("offerAudioEncoder","printing chunk #" + chunk + "of " + numChunks);
//Copy the data into the chunk array
if (chunk < input.length / CHUNKSIZE)
for (int i = 0; i < CHUNKSIZE; i++)
passMe[i] = input[chunk * CHUNKSIZE + i];
else {
eosReceived = true;
for (int i = 0; chunk * CHUNKSIZE + i < input.length; i++)
passMe[i] = input[chunk * CHUNKSIZE + i];
}

//Get the input buffer
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) {
while(inputBufferIndex < 0)//justk keep trying.
inputBufferIndex = mAudioEncoder.dequeueInputBuffer(100);
inputBuffer = mAudioEncoder.getInputBuffer(inputBufferIndex);
} else {
//backwards compatibility.
ByteBuffer[] inputBuffers = mAudioEncoder.getInputBuffers();
inputBufferIndex = mAudioEncoder.dequeueInputBuffer(-1);
if (inputBufferIndex >= 0)
inputBuffer = inputBuffers[inputBufferIndex];
}

//Plop the data into the input buffer
if (inputBuffer != null) {
inputBuffer.clear();
inputBuffer.put(passMe);
}
long presentationTimeUs = chunk * 10000000; //each encoded chunk represents one second of audio
//this is what the frame should be labeled as
mAudioEncoder.queueInputBuffer(inputBufferIndex, 0, passMe.length, presentationTimeUs, 0);

//Pull the output buffer.
int encoderStatus = -1;
while(encoderStatus < 0) //Like, seriously, WAIT forever.
encoderStatus = mAudioEncoder.dequeueOutputBuffer(mAudioBufferInfo, -1);//wait forever, why not?
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2)
outputBuffer = mAudioEncoder.getOutputBuffer(encoderStatus);
else {
ByteBuffer[] encoderOutputBuffers = mAudioEncoder.getOutputBuffers();
outputBuffer = encoderOutputBuffers[encoderStatus];
}

if(encoderStatus >= 0) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2)
mMuxer.writeSampleData(audioTrackIndex, outputBuffer, mAudioBufferInfo);

//Done with the output buffer, release it.
mAudioEncoder.releaseOutputBuffer(encoderStatus, false);
}//TODO: Add cases for what to do when the output format changes

最佳答案

好吧,我明白了。最终我放弃了分块逻辑,只是通过设置增加了输入缓冲区的大小

audioFormat.setInteger(MediaFormat.KEY_MAX_INPUT_SIZE, 14000000);

用于传递给 MediaCodec 的配置方法的 MediaFormat 对象。

还有一个很好的提示:确保使用 16 位音频编码并使用发出短裤的 AudioRecord.read 方法。字节似乎会产生奇怪的音频(可能是因为 AudioRecord 想要以 16 位运行)。

关于Android MediaCodec dequeueInputBuffer 总是返回 -1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41539669/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com