Skip to content Skip to sidebar Skip to footer

Control The Playback Speed Of Video In Android

I am using a VideoView to play a video file kept in res/raw. I couldnt find a way to control the playback speed of the video. Basically i want to reduce and increase the playback w

Solution 1:

you can use this but it works on api 23 and above

 mVideo.setOnPreparedListener(newMediaPlayer.OnPreparedListener() {
        @OverridepublicvoidonPrepared(MediaPlayer mp) {

            //works only from api 23PlaybackParamsmyPlayBackParams=null;
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
                myPlayBackParams = newPlaybackParams();
                myPlayBackParams.setSpeed(0.8f); //you can set speed here
                mp.setPlaybackParams(myPlayBackParams);
            }

        }
    });

Solution 2:

No, you cannot change the playback rate by simply using VideoView. VideoView and MediaPlayer only provide limited media functions.

You have to use some third party library, e.g., PVPlayer, and implement that yourself.

That's also why good media players on Android are so valuable:)

Solution 3:

I want to say than Mk Kamal's solution have an unexpected side effect: calling setPlaybackParams in OnPreparedListener will force VideoView to repeat the latest played video when the app was returned from the background. I don't know is it a bug or a feature, but I found a way to avoid such behavior:

privatefloatspeed=0.8f;
privatefinal MediaPlayer.OnInfoListenerlistener= (mp, what, extra) -> {

    if (what == MediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START) {
        mp.setPlaybackParams(mp.getPlaybackParams().setSpeed(speed));
        returntrue;
    }
    returnfalse;
};


videoView.setOnPreparedListener(
                mp -> {
                    mp.setOnInfoListener(listener);
                }
        );

MEDIA_INFO_VIDEO_RENDERING_START will be send only if the palyer was already started.

And I want to emphasize that getPlaybackParams is annotated as @NonNull, so it's not necessary to create new PlaybackParams object.

Solution 4:

DicePlayer works perfectly on my Asus Transformer. It has a speed control onscreen display.

I'm not sure what res/raw is though.

Post a Comment for "Control The Playback Speed Of Video In Android"