[Android]使用MediaCodec解码Mp3音频文件时始终显示“INFO_OUTPUT_FORMAT_CHANGED”状态

时间:2016-08-09 09:06:15

标签: android audio decode mediacodec

正如我在标题中所说,我正在尝试使用Android上的MediaCodec将Mp3文件解码为pcm文件,并且当我在SUMSANG S6(Android 6.0.1),MediaCodec上运行我的代码时,我在许多Device.But上取得了成功.INFO_OUTPUT_FORMAT_CHANGED总是通过MediaCodec.dequeueOutputBuffer()返回。我在开发人员doc中寻求解决方案,我得到的只是用getOutputFormat()获取新的MediaFormat。但我不知道我可以用新的MediaFormat做什么,如何继续从MediaCodec获取解码数据。我在捕获“I​​NFO_OUTPUT_FORMAT_CHANGED”后尝试继续循环,MediaCodec.dequeueOutputBuffer()的返回值始终为“INFO_OUTPUT_FORMAT_CHANGED”。我真的不知道如何处理它。谢谢你的回答!

`

    for (int i = 0; i < decodeInputBuffers.length - 1; i++) {
        int inputIndex = decoder.dequeueInputBuffer(-1);// get available inputBuffer
        // -1:wait always, 0:don't wait
        // suggest -1 ,avoid to lose frame
        if (inputIndex < 0) {
            codeOver = true;
            return;
        }
        // showLog("decodeInIndex:" + inputIndex);
        ByteBuffer inputBuffer = decodeInputBuffers[inputIndex];// get inputBuffer
        inputBuffer.clear();// clear the data before
        int sampleSize = mediaExtractor.readSampleData(inputBuffer, 0);// MediaExtractor read data into inputBuffer
        if (sampleSize < 0) {// less than 0,symbols all of the data has been read
            codeOver = true;
        } else {
            // long timepts = 1000000*i / 20;
            // showLog("timepts:" + timepts);
            decoder.queueInputBuffer(inputIndex, 0, sampleSize, 0, 0);// notice MediaDecode to decode the data input just now
            mediaExtractor.advance();// MediaExtractor move to the next sample point
        }
    }

    //get the decoded byte[] data 10000 is the waiting time
    //-1: wait always ,0: don't wait ,unit here is ms
    //don't suggest -1 here,because there is no data output sometimes,and it will  block here for long time waiting
    int outputIndex;
    ByteBuffer outputBuffer;
    byte[] chunkPCM;
    // outputIndex
    do{
        outputIndex = decoder.dequeueOutputBuffer(decodeBufferInfo,10000);
        if(outputIndex == MediaCodec.INFO_OUTPUT_BUFFERS_CHANGED) {
            showLog("OutPutBuffers has been changed!");
            decodeOutputBuffers = decoder.getOutputBuffers();
            continue;
        }
        if(outputIndex == MediaCodec.INFO_OUTPUT_FORMAT_CHANGED){
            showLog("OutPutFormat has been changed!");
            outputFormat = decoder.getOutputFormat();
            continue;
        }
        outputBuffer = decodeOutputBuffers[outputIndex];// get PCM data to stored
        chunkPCM = new byte[decodeBufferInfo.size];// BufferInfo defined the size of the data block
        outputBuffer.get(chunkPCM);// transfer the data from the Buffer into the byte array
        outputBuffer.clear();// remember to clear the Buffer after get data
        // MediaCodec use the Buffer in loop ,we will get the same data ,if we don't clear
        // putPCMData(chunkPCM);// put PCM data into container
        try {
            bos.write(chunkPCM,0,chunkPCM.length);
        } catch (IOException e) {
            e.printStackTrace();
        }
        decoder.releaseOutputBuffer(outputIndex, false);// operation necessary
        // otherwise MediaCodec can't output data after use data up
        outputIndex = decoder.dequeueOutputBuffer(decodeBufferInfo,
                10000);// get data again
        //if outputIndex=-1,there is no data output ,loop is over
    }while(outputIndex >=0);   //we can't determine the decoded data will be put out completely ,so we use while loop`

0 个答案:

没有答案