LoginSignup
0
0

More than 3 years have passed since last update.

MediaCodecで動画再生速度を調整する。

Posted at

MediaCodecで動画再生速度を調整する。

MediaFormatから captureRate など各種情報を取得する


            MediaFormat mediaFormat = null;
            for (int i = 0; i < nTracks; ++i) {
                mCodecWrapper = MediaCodecWrapper.fromVideoFormat(mExtractor.getTrackFormat(i),t_s);
                if (mCodecWrapper != null) {
                    mExtractor.selectTrack(i);
                    mediaFormat = mExtractor.getTrackFormat(i);
                    break;
                }
            }
            if( mediaFormat !=null) {
                totalTime = mediaFormat.getLong(MediaFormat.KEY_DURATION);
                captureRate = mediaFormat.getInteger(MediaFormat.KEY_FRAME_RATE);
                mMovieWidth = mediaFormat.getInteger(MediaFormat.KEY_WIDTH);
                mMovieHeight = mediaFormat.getInteger(MediaFormat.KEY_HEIGHT);
            }



    public int startPlayback() {
        int update = 0;
        try {
            boolean isEos = ((mExtractor.getSampleFlags() & MediaCodec
                    .BUFFER_FLAG_END_OF_STREAM) == MediaCodec.BUFFER_FLAG_END_OF_STREAM);

            // BEGIN_INCLUDE(write_sample)
            if (!isEos) {
                // Try to submit the sample to the codec and if successful advance the
                // extractor to the next available sample to read.
                long sampletime = mExtractor.getSampleTime();
                int flag = mExtractor.getSampleFlags();

                boolean result = mCodecWrapper.writeSample(mExtractor, false, sampletime,flag);
                //boolean result = mCodecWrapper.writeSample(mExtractor, false, mExtractor.getSampleTime(), mExtractor.getSampleFlags());

                if (result) {
                    // Advancing the extractor is a blocking operation and it MUST be
                    // executed outside the main thread in real applications.
                    mExtractor.advance();
                }
            }
            // END_INCLUDE(write_sample)

            // Examine the sample at the head of the queue to see if its ready to be
            // rendered and is not zero sized End-of-Stream record.
            MediaCodec.BufferInfo out_bufferInfo = new MediaCodec.BufferInfo();
            mCodecWrapper.peekSample(out_bufferInfo);

            // BEGIN_INCLUDE(render_sample)
            if (out_bufferInfo.size <= 0 && isEos) {
                //mTimeAnimator.end();
                //mCodecWrapper.stopAndRelease();
                //mExtractor.release();
                //reset();
                mExtractor.seekTo(0, MediaExtractor.SEEK_TO_CLOSEST_SYNC);
            } else if (out_bufferInfo.presentationTimeUs / 1000 < totalTime) {
                // Pop the sample off the queue and send it to {@link Surface}
                update = mCodecWrapper.popSample(true);
                if( update>0){
                    try {
                        Thread.sleep((int)1000/captureRate);  //ここで再生速度の調整をしている
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
            // END_INCLUDE(render_sample)

        }catch(Exception e) {
            LogOut.printf(e.toString());
        }
        return update;
    }

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0