如何在ExoPlayer.STATE_ENDED之后在Exoplayer上重新启动视频

时间:2015-08-14 08:42:38

标签: android exoplayer

我正在使用 Exoplayer ,其基础为 DemoPlayer 。 我希望重新启动/重播视频,从 发送 ExoPlayer.STATE_ENDED 后的某些用户操作开始。

我之后尝试使用seekTo(0)mPlayer.setPlayWhenReady(true);,但根本没有做任何事情。

4 个答案:

答案 0 :(得分:19)

我已将库更新到ExoPlayer r1.4.2并完成了工作......

    mPlayer.seekTo(0);
    mPlayer.setPlayWhenReady(true); // replay from start

//重启后暂停视频

    mPlayer.seekTo(0);
    mPlayer.setPlayWhenReady(false);

答案 1 :(得分:2)

可以使用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最新版本是否为最新版本:

Exoplayer Releases

答案 2 :(得分:1)

此代码对我有用

 @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);
}

答案 3 :(得分:-1)

以上代码是从exo原始库复制而来的,它还调用了seekTo()方法 enter image description here