我正在尝试从 AudioRecord 对象中获取原始数据,并使用 MediaMuxer 和 MediaCodec 将其保存在文件中。
我启动编解码器,启动复用器,将数据加载到输入缓冲区中,但没有这样的运气。
通过调试调查,我发现问题出现在对dequeueInputBuffer()
. 前几块数据似乎成功了,但最终dequeueInputBuffer()
只是-1
不断地返回。
我有什么明显的遗漏吗?这似乎是我正在填充输入缓冲区,但编解码器从未释放它们。
相关代码片段:
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