Friday, November 10, 2017

Android 6.0 - Set video speed with PlaybackParams

Leave a Comment

I have problems about how to implement PlaybackParams to set video speed:

public PlaybackParams getPlaybackParams ()

Added in API level 23
Gets the playback rate using PlaybackParams.

PlaybackParams setSpeed (float speed) //Sets the speed factor. 

Returns:
the playback rate being used.
Throws IllegalStateException:
if the internal sync engine or the audio track has not been initialized.

This is my code:

mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener()  {     @Override     public void onPrepared(MediaPlayer mp)      {         mp.setPlaybackParams(new PlaybackParams().setSpeed(1.f));          if (mPlaybackState == PlaybackState.PLAYING) { mVideoView.start();}     } }); 

3 Answers

Answers 1

You are getting an IllegalStateException while calling the 'setPlayParams' method, because you're not doing PlaybackParams params = mp.getPlaybackParams(), set the speed and then pass it to mp.setPlaybackParams()! Set the speed DIRECTLY while calling the mp.getPlayParams()!

MediaPlayer mp = ...;  float speed = 0.55f;      mp.setPlaybackParams(mp.getPlaybackParams().setSpeed(speed)); 

Let me now if it is useful!

Answers 2

my old account got deleted and cannot be restored so I repost my answer in case you want to accept or upvote this answer do it here.

You are getting an IllegalStateException while calling the 'setPlayParams' method, because you're not doing PlaybackParams params = mp.getPlaybackParams(), set the speed and then pass it to mp.setPlaybackParams()! Set the speed DIRECTLY while calling the mp.getPlayParams()!

MediaPlayer mp = ...;  float speed = 0.55f;      mp.setPlaybackParams(mp.getPlaybackParams().setSpeed(speed)); 

Answers 3

After many try i find a solution.

Example how use VideoView

final VideoView mVideoView = findViewById(R.id.videoView); mVideoView.setVideoPath(Environment.getExternalStorageDirectory() + "/bluetooth/test.webm"); //Path of your file video mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {     @Override     public void onPrepared(MediaPlayer mp)     {         mp.setPlaybackParams(new PlaybackParams().setSpeed(0.55f));         mVideoView.start();     } }); MediaController media = new MediaController(this); //this is for play and restart play manually media.setAnchorView(mVideoView); mVideoView.setMediaController(media); //mVideoView.start(); 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment