如何在Exoplayer中暂停/恢复视频播放?

4

我正在使用 Exoplayer 播放视频。

播放视频的代码


private PlayerView videoView;
SimpleExoPlayer player;

    videoView = findViewById(R.id.video_view);
    player = ExoPlayerFactory.newSimpleInstance(this, new DefaultTrackSelector());
    videoView.setPlayer(player);
    player.prepare(buildMediaSource(Uri.parse(_videoUrl)));
    player.setPlayWhenReady(true);

    videoView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            //i want to pause video
            //next time i want to resume
            return false;
        }
    });

XML 代码


<com.google.android.exoplayer2.ui.PlayerView
    android:id="@+id/video_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginBottom="8dp"
    //i don't want to use controls to i am setting false
    app:use_controller="false"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

我找不到有用的文档来暂停和恢复视频播放

我的需求是在点击VideoView时,暂停/恢复视频,有人知道怎么做吗?

依赖

implementation 'com.google.android.exoplayer:exoplayer:2.7.3'
implementation 'com.google.android.exoplayer:exoplayer-core:2.7.3'
implementation 'com.google.android.exoplayer:exoplayer-dash:2.7.3'
implementation 'com.google.android.exoplayer:exoplayer-ui:2.7.3'

1
使用 setPlayWhenReady() - false 暂停,true - 恢复播放。 - Gautam
@Gautam 谢谢,它正在工作。 - Ashvin solanki
@Gautam,你能提供答案吗?我正在使用以下代码:`videoView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { player.setPlayWhenReady(!player.getPlayWhenReady()); return false; } });` - Ashvin solanki
1个回答

10

视频播放

private void playPlayer() {
    if (player != null) {
        player.setPlayWhenReady(true);
    }
}

暂停视频

private void pausePlayer() {
    if (player != null) {
        player.setPlayWhenReady(false);
    }
}

从特定位置开始

private void seekTo(long positionInMS) {
    if (player != null) {
        player.seekTo(positionInMS);
    }
}

释放播放器
private void releasePlayer() {
    if (player != null) {
        player.release();
    }
}

视频暂停的代码或逻辑在哪里?我在提供的代码中找不到...你能解释得更详细一些吗? - Ashvin solanki
1
私有的void playPlayer() { 如果(player != null) { player.setPlayWhenReady(true); } }私有的void pausePlayer() { 如果(player != null) { player.setPlayWhenReady(false); } }私有的void seekTo(long positionInMS) { 如果(player != null) { player.seekTo(positionInMS); } }私有的void releasePlayer() { 如果(player != null) { player.release(); } } - Aravind V

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接