How to restart video on Exoplayer after ExoPlayer.STATE_ENDED
This code works for me
@OnClick(R.id.image_button_play)
public void play(){
Log.d(TAG, "play: clicked");
//If video has finished then set Exoplayer to 0
if (simpleExoPlayer.getPlaybackState() == Player.STATE_ENDED){
simpleExoPlayer.seekTo(0);
}
simpleExoPlayer.setPlayWhenReady(true);
playButton.setVisibility(View.INVISIBLE);
}
I've updated the library to ExoPlayer r1.4.2
and it does the job...
mPlayer.seekTo(0);
mPlayer.setPlayWhenReady(true); // replay from start
// Pause video after restart
mPlayer.seekTo(0);
mPlayer.setPlayWhenReady(false);
A video can be seamlessly looped using a LoopingMediaSource. The following example loops a audio/video indefinitely. It’s also possible to specify a finite loop count when creating a LoopingMediaSource.
MediaSource source = new ExtractorMediaSource(audioUri, ...);
// Loops the audio indefinitely.
LoopingMediaSource loopingSource = new LoopingMediaSource(source);
or add a listener
playerExo.addListener(new ExoPlayer.Listener() {
@Override
public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
switch(playbackState) {
case ExoPlayer.STATE_BUFFERING:
break;
case ExoPlayer.STATE_ENDED:
playerExo.seekTo(0);
break;
case ExoPlayer.STATE_IDLE:
break;
case ExoPlayer.STATE_PREPARING:
break;
case ExoPlayer.STATE_READY:
break;
default:
break;
}
}
@Override
public void onPlayWhenReadyCommitted() {
}
@Override
public void onPlayerError(ExoPlaybackException error) {
}
});
playerExo.seekTo(0);
playerExo.setPlayWhenReady(true);//replay from start
/* If you want to Pause audio/video and restart
mPlayer.seekTo(0);
mPlayer.setPlayWhenReady(false);*/
You can check the Exoplayer latest release to be up to date:
Exoplayer Releases