android - 如何在recyclerview中控制Exoplayer
问题描述
我在嵌套滚动视图内的回收器视图中添加了 exoplayer。播放器已初始化。但我遇到了一些问题。
每当我更改片段或滚动到底部(嵌套滚动)时,视频都会在后台播放。
当我更改活动并恢复相同时,所有视频都会自动开始播放。
这是主要布局
<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;
}
}
解决方案
嗨,不能发表评论,因为我的代表太低了。我认为Recycler 侦听器将帮助滚动屏幕继续播放的视图,因为当持有人回收时,您将能够获得播放器。随着片段的变化,我没有' t真的理解这个问题。
推荐阅读
- mysql - 比较两个表中的值并计算它们的差异
- virtual-machine - 从 VM 父级取消链接 VirtualBox VM 克隆
- php - Laravel 关系复合主键
- python - Django 容器并不完全在等待一个新的 postgres 容器
- vega-lite - 如何使刻度宽度更大,以便看到所有刻度内容
- api - AKS API 负载测试错误:内容长度分隔的消息正文过早结束
- python - Python语法问题(带列表的字符串)
- python - 如何在 .py 文件中提取一种类型(字符串,int ...)的所有变量?
- r - 如何解决功能 googler 的问题?
- kotlin - 如何在 Kotlin 1.4 中将 Double(或 Float)转换为 Byte(或 Short)?