在RecyclerView中发布ExoPlayer
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在RecyclerView中发布ExoPlayer相关的知识,希望对你有一定的参考价值。
我正在RecyclerView中实现ExoPlayer。但滚动视频时会停止,但音频不会停止。
如何在RecyclerView中发布ExoPlayer?或者我如何获得Recycled Object的位置以便我能够发布ExoPlayer?
这是我的数据对象
public class enter code here Video {
private String url;
private Context context;
Boolean playWhenReady = false;
int currentWindow = 0;
long playbackPosition = 0;
ExoPlayer player;
public Video(String url, Context context) {
this.url = url;
this.context = context;
}
public void setCurrentWindow(int currentWindow) {
this.currentWindow = currentWindow;
}
public void setPlaybackPosition(long playbackPosition) {
this.playbackPosition = playbackPosition;
}
public void setPlayWhenReady(Boolean playWhenReady) {
this.playWhenReady = playWhenReady;
}
public void setPlayer(ExoPlayer player) {
this.player = player;
}
public ExoPlayer getPlayer() {
return player;
}
public String getUrl() {
return url;
}
public Context getContext() {
return context;
}
public Boolean getPlayWhenReady() {
return playWhenReady;
}
public int getCurrentWindow() {
return currentWindow;
}
public long getPlaybackPosition() {
return playbackPosition;
}
}
这是我的适配器类:我尝试使用onViewRecycled()方法,但因为我没有得到位置我不能释放播放器。
public class VideoAdapter extends RecyclerView.Adapter<VideoAdapter.ViewHolder> {
private ArrayList<Video> mDataset;
public static class ViewHolder extends RecyclerView.ViewHolder {
public PlayerView mExoPlayer;
public ViewHolder(View itemView) {
super(itemView);
mExoPlayer = itemView.findViewById(R.id.video_view);
}
}
public VideoAdapter(ArrayList<Video> myDataset) {
mDataset = myDataset;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView=LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_item, parent, false);
return new ViewHolder(itemView);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
initPlayer(holder,position);
}
private void initPlayer(ViewHolder holder,int position) {
mDataset.get(position).setPlayer(ExoPlayerFactory.newSimpleInstance(
new DefaultRenderersFactory(mDataset.get(position).getContext()),
new DefaultTrackSelector(), new DefaultLoadControl()));
holder.mExoPlayer.setPlayer(mDataset.get(position).getPlayer());
mDataset.get(position).getPlayer().setPlayWhenReady(mDataset.get(position).getPlayWhenReady());
mDataset.get(position).getPlayer().seekTo(mDataset.get(position).getCurrentWindow(), mDataset.get(position).getPlaybackPosition());
Uri uri = Uri.parse(mDataset.get(position).getUrl());
MediaSource mediaSource = buildMediaSource(uri);
mDataset.get(position).getPlayer().prepare(mediaSource, true, false);
}
private MediaSource buildMediaSource(Uri uri) {
return new ExtractorMediaSource.Factory(
new DefaultHttpDataSourceFactory("exoplayer-codelab")).
createMediaSource(uri);
}
@Override
public int getItemCount() {
return mDataset.size();
}
@Override
public void onViewRecycled(@NonNull ViewHolder holder) {
super.onViewRecycled(holder);
}
}
答案
我认为,当它的视图被回收时释放播放器是最干净的。
适配器类:
@Override
public void onViewRecycled(@NonNull ViewHolder holder) {
int position = holder.getAdapterPosition();
if (mDataset.get(position) != null) {
mDataset.get(position).getPlayer().release();
}
super.onViewRecycled(holder);
}
以上是关于在RecyclerView中发布ExoPlayer的主要内容,如果未能解决你的问题,请参考以下文章
Recyclerview 中的 Exoplayer 错误,源错误没有可用的提取器
如何用recyclerview和viewpager2实现exoplayer?
RecyclerView 和 Firebase Storage 快速滚动 ExoPlayer 异常