我正在RecyclerView中实现ExoPlayer。但是在滚动时,视频停止,但音频停止。
如何在RecyclerView中释放ExoPlayer?或者如何获取回收对象的位置,以便能够释放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);
}
}
答案 0 :(得分:2)
我认为,当回收播放器的视图时,最好将其释放。
适配器类:
@Override
public void onViewRecycled(@NonNull ViewHolder holder) {
int position = holder.getAdapterPosition();
if (mDataset.get(position) != null) {
mDataset.get(position).getPlayer().release();
}
super.onViewRecycled(holder);
}
答案 1 :(得分:0)
系统不能容纳多个ExoPlayer
实例。它可以抛出OOM Exception
。因此,当用户看不到该项目时,必须释放ExoPlayer
。在这种情况下,建议您使用RecyclerView.Adapter
override fun onViewDetachedFromWindow(holder: ViewHolder) {
holder.itemView.player?.stop()
holder.itemView.player?.release()
holder.itemView.player = null
super.onViewDetachedFromWindow(holder)
}
然后,在ViewHolde
r类中,您应该检查是否player == null
,必须对其进行初始化。