6

我正在尝试从 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
4

1 回答 1

2

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

audioFormat.setInteger(MediaFormat.KEY_MAX_INPUT_SIZE, 14000000);

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

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

于 2017-01-12T19:11:50.057 回答