MediaCodec outputBuffer始终为空

时间:2017-09-26 12:23:38

标签: android mediacodec mediaextractor

我的outputBuffer回调方法中的onOutputBufferAvailable()存在问题。每次调用此回调方法时,我都会使用outputBuffer方法检查outputBuffer.hasRemaining,并始终返回false

        codec.setCallback(new MediaCodec.Callback() {

            @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
            @Override
            public void onInputBufferAvailable(MediaCodec mc, int inputBufferId) {

                ByteBuffer inputBuffer = codec.getInputBuffer(inputBufferId);
                int rounds = extractor.readSampleData(inputBuffer,0);

                if(rounds > 0) {
                    long currentT = extractor.getSampleTime();
                    Log.i("CurrentT", String.valueOf(currentT/(1000*1000)));
                    codec.queueInputBuffer(inputBufferId, 0, rounds, 0, 0);
                     extractor.advance();


                } else {
                    // EOS
                    codec.queueInputBuffer(inputBufferId, 0, 0, 0, MediaCodec.BUFFER_FLAG_END_OF_STREAM);
                    Log.i("EOS","=-1");


                }


            }

            @Override
            public void onError(@NonNull MediaCodec mediaCodec, @NonNull MediaCodec.CodecException e) {
                Log.i("error", "e");
            }
            @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
            @Override
            public void onOutputBufferAvailable(MediaCodec mc, int outputBufferId, MediaCodec.BufferInfo info) {
                ByteBuffer outputBuffer = codec.getOutputBuffer(outputBufferId);

                Log.i("has", String.valueOf(outputBuffer.hasRemaining()));
                if (outputBuffer.hasRemaining()) {

                    b = outputBuffer.get();
                    Log.i("i", String.valueOf(b));
                    BAOS.write(b);
                    outputBuffer.clear();

                } else {

                    array= BAOS.toByteArray();
                    codec.stop();
                    codec.release();

                }
                 codec.releaseOutputBuffer(outputBufferId, false);

            }


            @Override
            public void onOutputFormatChanged(MediaCodec mc, MediaFormat format) {
                // Subsequent data will conform to new format.
                // Can ignore if using getOutputFormat(outputBufferId)
               // mOutputFormat = format; // option B
            }
        });

2 个答案:

答案 0 :(得分:1)

MediaCodec不会更新ByteBuffers上的位置/限制标记,您需要检查MediaCodec.BufferInfo对象,您必须知道实际有效载荷数据的位置和长度。

答案 1 :(得分:0)

问题的主要原因是错误MediaCodec模式;是MediaCodec.createByCodecName(...),应该是codec = MediaCodec.createDecoderByType("audio/mpeg")。在unsuccesfull尝试使用get()获取字节后,我最终得到了get(byte[] dst)方法。下面的工作代码。

            @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
            @Override
            public void onOutputBufferAvailable(MediaCodec mc, int outputBufferId, MediaCodec.BufferInfo info) {
                ByteBuffer outputBuffer = codec.getOutputBuffer(outputBufferId);

                    byte[] b = new byte[outputBuffer.remaining()];
                    outputBuffer.get(b);

                    for(i=0;i< b.length;i++) {
                        BAOS.write(b[i]);
                    }
                    outputBuffer.clear();

                codec.releaseOutputBuffer(outputBufferId, false);

                if (info.flags == MediaCodec.BUFFER_FLAG_END_OF_STREAM) {
                    array=BAOS.toByteArray();
                    Log.i("array", String.valueOf(array.length));
                    FileTools fl = new FileTools();
                    fl.WriteFile(array);
                    codec.stop();
                    codec.release();
                }
            }