我正在使用带有 DemoPlayer基础的Exoplayer。在调度ExoPlayer.STATE_ENDED之后,我想在某些用户操作上重新开始/重播视频。
我尝试过使用seekTo(0)
和mPlayer.setPlayWhenReady(true);
之后,但它根本没有做任何事情。
我已将库更新为 ExoPlayer r1.4.2
,它完成了这项工作......
mPlayer.seekTo(0);
mPlayer.setPlayWhenReady(true); // replay from start
// 重启后暂停视频
mPlayer.seekTo(0);
mPlayer.setPlayWhenReady(false);
可以使用 LoopingMediaSource 无缝循环视频。以下示例无限循环音频/视频。创建 LoopingMediaSource 时也可以指定有限循环计数。
MediaSource source = new ExtractorMediaSource(audioUri, ...);
// Loops the audio indefinitely.
LoopingMediaSource loopingSource = new LoopingMediaSource(source);
或添加监听器
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);*/
您可以检查 Exoplayer 最新版本是否为最新版本:
这段代码对我有用
@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);
}