我正在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);
}
}
最佳答案
我认为在回收播放器的 View 后释放它是最干净的方法。
适配器类:
@Override
public void onViewRecycled(@NonNull ViewHolder holder) {
int position = holder.getAdapterPosition();
if (mDataset.get(position) != null) {
mDataset.get(position).getPlayer().release();
}
super.onViewRecycled(holder);
}
关于android - 在RecyclerView中发布ExoPlayer,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51686098/