Android Exoplayer 'Forward', 'Rewind' programmatically

Viewed 8446

I have exoplayer in my Android app and want to do 'Forward', 'Rewind' programmatically and these two button are not in the control view layout.

This two-button are outside exoplayer view, but in the screen and when user will click on "Forward" or "Rewind" it will work accordingly.

Some blogs given solution but all are for control view, example:

app:fastforward_increment="30000"
app:rewind_increment="30000"

But in my case button are completely outside of the player view.

The same question was asked earlier here, but there was no proper solution.

Screenshot attached: enter image description here

Tried following code but not getting any impact:

//switch case onClick listener for 'Forward' and 'Rewind'
case R.id.text_view_rewind:
    simpleExoPlayerView.setRewindIncrementMs(5000);
    break;
case R.id.text_view_forward:
     simpleExoPlayerView.setFastForwardIncrementMs(5000);
     break;
5 Answers

for the Kotlin crew:

player.seekTo(player.currentPosition + 5000)

Important thing to note: you don't have to worry about going outside the bounds of the video. For example, if you hit rewind 10 seconds but you're only at second 5 of the video, it won't go to -5 seconds, it will go to 0. Same with pressing fast forward at the end of the video.

With version 2.16.1 you can set like this:

val player = ExoPlayer.Builder(this)
            .setSeekBackIncrementMs(10000)
            .setSeekForwardIncrementMs(10000)
            .build()

If I understood this problem correctly, you want a fast forward and fast rewind option to keep on fast forwarding/rewinding the content at 2x or 4x or 8x speed till external play action is not performed.

If this is what you want to do then you need to do 3 things:

  1. Find current playback position
  2. Based on 2x 4x 8x speed calculate the next playback position at which video should jump
  3. Keep on performing step 1 and 2 till play is not called.

Translating this theory into code will look like:

// current playback position 
var startPosition = player.currentPosition

// start time, usually -> System.currentTimeMillis()
var startTime = 0

// Speed representing playback rate 
var speed = 11

inner class FastForwardRunnable : Runnable {

    override fun run() {

        var nextPosition = startPosition.plus((System.currentTimeMillis() - startTime) * speed)

        if (nextPosition > metadata.duration) {
            nextPosition = metadata.duration
        }

        player.seekTo(nextPosition)
    }
}

Handler is suggested for continuous looping in here as it is easy to handle and can handle all these UI operations on UI Thread. Hope this helps and let me know if there is any question related to this.

Note: This is a Pseudo code and is presented here just for illustration. Fill in all the values and try.

You need to use seekTo(Position)

 int p = player.getCurrentPosition();
 p = p + 5000; // for rewind use -5000
 player.seekTo(p);

Then update progress.

If anyone wants to add rewind and forward feature on their video player or using ExoPlayer2 then this code might help you,

Just add this code on your button click listener.

//switch case onClick listener for 'Forward' and 'Rewind'

case R.id.exo_rewind:
    try {
        player.seekTo(player.getCurrentPosition() - 10000); // 10000 = 10 Seconds
    } catch (Exception e) {
        Toast.makeText(this, "Error : " + e.toString(), Toast.LENGTH_SHORT).show();
    }
    break;

    case R.id.exo_forword:
        try {
            player.seekTo(player.getCurrentPosition() + 10000); // 10000 = 10 Seconds
        } catch (Exception e) {
            Toast.makeText(this, "Error : " + e.toString(), Toast.LENGTH_SHORT).show();
        }
        break;

OR

//switch case onClick listener for 'Forward' and 'Rewind'

case R.id.exo_rewind:
    int rewind = (int) player.getCurrentPosition();
    rewind = rewind - 10000; // 10000 = 10 Seconds
    player.seekTo(rewind);
    break;
case R.id.exo_forward:
    int forward = (int) player.getCurrentPosition();
    forward = forward + 10000; // 10000 = 10 Seconds
    player.seekTo(forward);
    break;
Related