如何在recyclerview中控制Exoplayer

问题描述 投票:0回答:1

我在嵌套滚动视图内的回收器视图中添加了exoplayer。播放器已初始化。但与此同时我遇到了一些问题。

  1. [无论何时我更改片段或滚动到底部(嵌套滚动),视频都会在后台播放。

  2. 当我更改活动并恢复为相同状态时,所有视频都会自动开始播放。

这里是主要布局

<androidx.coordinatorlayout.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
  <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
      android:layout_width="match_parent"
      android:id="@+id/swipe"
      android:background="?android:attr/windowBackground"
      android:layout_height="match_parent">

    <androidx.core.widget.NestedScrollView
        android:id="@+id/nestedScroll"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".fragment.ForYou">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:paddingTop="@dimen/_5sdp">

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/recyclerview"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                </androidx.recyclerview.widget.RecyclerView>

        </LinearLayout>
    </androidx.core.widget.NestedScrollView>
  </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

这里是子布局

<LinearLayout
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_marginBottom="@dimen/_8sdp"
    android:layout_width="match_parent"
    android:src="@drawable/backgroundgraident"
    android:layout_height="wrap_content">

            <com.google.android.exoplayer2.ui.PlayerView
                android:id="@+id/video_view"
                android:layout_width="match_parent"
                android:layout_height="@dimen/_250sdp"
                android:layout_marginTop="20dp"

                android:background="#000000"
                android:clickable="true"
                android:visibility="gone"
                app:bar_height="@dimen/_1sdp"
                app:use_controller="true" />

</LinearLayout>

Java代码

 private void initializePlayer(MyviewHolder hol , int pos) {

        player = ExoPlayerFactory.newSimpleInstance(context);

        MediaSource mediaSource = buildMediaSource( mediaUri );

        player.setPlayWhenReady(playWhenReady);
        player.seekTo(currentWindow, playbackPosition);
        player.prepare(mediaSource, false, false);

        player.setVideoScalingMode( C.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING);
        playerView.setControlDispatcher( new MyDefaultControlDispatcher() );

        playerView.setPlayer(player);

}

private MediaSource buildMediaSource(Uri uri) {

    DataSource.Factory dataSourceFactory =
            new DefaultDataSourceFactory(context, "exoplayer-codelab");
    return new ProgressiveMediaSource.Factory(dataSourceFactory)
            .createMediaSource(uri);
}

 public void releasePlayer() {
        if (player != null) {

            if(player.isPlaying()) {
                player.stop();
            }

            playWhenReady = player.getPlayWhenReady();
            playbackPosition = player.getCurrentPosition();
            currentWindow = player.getCurrentWindowIndex();
            player.release();
            player = null;
        }
    }
android android-recyclerview exoplayer android-nestedscrollview
1个回答
0
投票
© www.soinside.com 2019 - 2024. All rights reserved.