使用MediaSync和MediaExtractor,播放速度提高2倍

时间:2015-11-05 06:14:11

标签: android android-6.0-marshmallow mediaextractor

我正在使用MediaSync和MediaExtractor播放视频。

要开始播放我已写下面的代码

 MediaSync mediaSync = new MediaSync();
            mediaSync.setSurface(surface);
            Surface inputSurface = mediaSync.createInputSurface();

             mediaExtractor = new MediaExtractor();

            try {
                mediaExtractor.setDataSource(this.clipPath);
            } catch (IOException e1) {
                e1.printStackTrace();
            }


            for (int i = 0; i < mediaExtractor.getTrackCount(); i++) {
                MediaFormat format = mediaExtractor.getTrackFormat(i);
                String mime = format.getString(MediaFormat.KEY_MIME);
                if (mime.startsWith("video/")) {
                    Log.d("Med","Formt data:" +format.toString());
                    Log.d("Med","frame  data:" +format.getInteger(MediaFormat.KEY_FRAME_RATE));
                    ratio=format.getInteger(MediaFormat.KEY_FRAME_RATE);
//                    Log.d("Med","capture  data:" +format.getInteger(MediaFormat.KEY_CAPTURE_RATE));

                            mediaExtractor.selectTrack(i);
//                    ratio=1000f/format.getInteger(MediaFormat.KEY_FRAME_RATE);
                    try {
                        videoDecoder = MediaCodec.createDecoderByType(mime);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    videoDecoder.configure(format, inputSurface, null, 0);
                    Log.d(LOG_TAG, "Found a video track.");
                    break;
                }
            }

            SyncParams syncParams = new SyncParams();
            syncParams.allowDefaults();

            mediaSync.setPlaybackParams(new PlaybackParams().allowDefaults());
            mediaSync.setSyncParams(syncParams);

            videoDecoder.setCallback(decoderCallback, null);
            videoDecoder.start();

我使用下面的代码进行解码器回调:

MediaCodec.Callback decoderCallback = new MediaCodec.Callback() {
            @Override
            public void onInputBufferAvailable(MediaCodec codec, int index) {
                if (index >= 0) {
                    ByteBuffer byteBuffer = codec.getInputBuffer(index);
                    int sampleSize = mediaExtractor.readSampleData(byteBuffer, 0);
                    Log.d(LOG_TAG, "SampleSize: " + sampleSize);
                    if (sampleSize < 0) {
                        //we're at end of file so submit EOS to the decoder input
                        Log.d(LOG_TAG, "Video Decoder input EOS reached");
                        codec.queueInputBuffer(index, 0, 0, 0, MediaCodec.BUFFER_FLAG_END_OF_STREAM);
                    } else {
                        long sampleTime = mediaExtractor.getSampleTime();
                        Log.d(LOG_TAG  ,"ratio"+ratio+" sampletime"+sampleTime);
                        codec.queueInputBuffer(index, 0, sampleSize, sampleTime, 0);
                        mediaExtractor.advance();
                    }
                }
            }

            @Override
            public void onError(MediaCodec codec, MediaCodec.CodecException e) {

            }



               @Override
                public void onOutputFormatChanged(MediaCodec codec, MediaFormat format) {

                }

                @Override
                public void onOutputBufferAvailable(MediaCodec codec, int index, MediaCodec.BufferInfo info) {
                    Log.d(LOG_TAG, "Rendering with preso time: " + info.presentationTimeUs);
                    codec.releaseOutputBuffer(index, info.presentationTimeUs);

                }
            };

如果我在onOutputBufferAvailable中修改了代码,它将以正常的播放速度播放,

@Override
            public void onOutputBufferAvailable(MediaCodec codec, int index, MediaCodec.BufferInfo info) {
                Log.d(LOG_TAG, "Rendering with preso time: " + info.presentationTimeUs);
                codec.releaseOutputBuffer(index, info.presentationTimeUs+(ratio+1000000L));
                try {

                    Thread.sleep((int)(1000/ratio));
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

这不是正确的做法。

如果有人可以帮我找到正常速度播放视频的正确方法。

1 个答案:

答案 0 :(得分:1)

不确定这是否可以解决您的问题,但您对releaseOutputBuffer的调用似乎具有以微秒为单位的时间戳。查看MediaSync文档中的示例,显示此时间戳需要以纳秒为单位。所以你需要乘以info.presentationTimeUs * 1000。

onOutputBufferAvailable(MediaCodec codec, int bufferId, BufferInfo info) {
 // ...
 if (codec == videoDecoder) {
     // surface timestamp must contain media presentation time in nanoseconds.
     codec.releaseOutputBuffer(bufferId, 1000 * info.presentationTime);
 } else {
     ByteBuffer audioByteBuffer = codec.getOutputBuffer(bufferId);
     sync.queueAudio(audioByteBuffer, bufferId, info.presentationTime);
 }
 // ...

}

http://developer.android.com/reference/android/media/MediaSync.html