当我回到上一个视图 ViewPager2 时,ExoPlayer 不播放 onViewRecycled
ExoPlayer not playing onViewRecycled when I back to the previous view ViewPager2
我正在使用带有回收器视图适配器的 ViewPager2,并按照如下所示进行设置
当我滑动屏幕并更改索引时,它对新索引工作正常,但是当我转到以前的索引时,它不会播放任何视频,为什么?有什么建议吗??
RecylcerViewAdapter.java
@Override
public void onViewDetachedFromWindow(@NonNull VideoPlayerViewHolder holder) {
super.onViewDetachedFromWindow(holder);
holder.pausePlayer();
holder.releasePlayer();
}
@Override
public void onViewAttachedToWindow(@NonNull VideoPlayerViewHolder holder) {
super.onViewAttachedToWindow(holder);
holder.startPlayer();
}
@Override
public void onViewRecycled(@NonNull VideoPlayerViewHolder holder) {
super.onViewRecycled(holder);
holder.startPlayer();
}
@Override
public void onBindViewHolder(@NonNull VideoPlayerViewHolder holder, int position) {
holder.setExoplayer(getItem(position).getUrl());
}
ViewHolderr.java
public void startPlayer() {
player.setPlayWhenReady(true);
player.getPlaybackState();
}
public void pausePlayer() {
player.setPlayWhenReady(false);
player.getPlaybackState();
}
public void setExoplayer(String url) {
player = new SimpleExoPlayer.Builder(itemView.getContext()).build();
player.setRepeatMode(Player.REPEAT_MODE_ONE);
dataSourceFactory = new DefaultDataSourceFactory(itemView.getContext(),"exo-player");
mediaSource = new ProgressiveMediaSource.Factory(dataSourceFactory).createMediaSource(Uri.parse(url));
player.prepare(mediaSource);
playerView.setPlayer(player); //need to transfer
player.seekTo(playbackPosition);
player.addListener(this);
}
public void releasePlayer() {
player.setPlayWhenReady(false);
player.release();
player.stop(true);
}
像这样附加视图时设置 Exoplayer
@Override
public void onViewAttachedToWindow(@NonNull VideoPlayerViewHolder holder) {
super.onViewAttachedToWindow(holder);
holder.setExoplayer();
holder.startPlayer();
}
我正在使用带有回收器视图适配器的 ViewPager2,并按照如下所示进行设置
当我滑动屏幕并更改索引时,它对新索引工作正常,但是当我转到以前的索引时,它不会播放任何视频,为什么?有什么建议吗??
RecylcerViewAdapter.java
@Override
public void onViewDetachedFromWindow(@NonNull VideoPlayerViewHolder holder) {
super.onViewDetachedFromWindow(holder);
holder.pausePlayer();
holder.releasePlayer();
}
@Override
public void onViewAttachedToWindow(@NonNull VideoPlayerViewHolder holder) {
super.onViewAttachedToWindow(holder);
holder.startPlayer();
}
@Override
public void onViewRecycled(@NonNull VideoPlayerViewHolder holder) {
super.onViewRecycled(holder);
holder.startPlayer();
}
@Override
public void onBindViewHolder(@NonNull VideoPlayerViewHolder holder, int position) {
holder.setExoplayer(getItem(position).getUrl());
}
ViewHolderr.java
public void startPlayer() {
player.setPlayWhenReady(true);
player.getPlaybackState();
}
public void pausePlayer() {
player.setPlayWhenReady(false);
player.getPlaybackState();
}
public void setExoplayer(String url) {
player = new SimpleExoPlayer.Builder(itemView.getContext()).build();
player.setRepeatMode(Player.REPEAT_MODE_ONE);
dataSourceFactory = new DefaultDataSourceFactory(itemView.getContext(),"exo-player");
mediaSource = new ProgressiveMediaSource.Factory(dataSourceFactory).createMediaSource(Uri.parse(url));
player.prepare(mediaSource);
playerView.setPlayer(player); //need to transfer
player.seekTo(playbackPosition);
player.addListener(this);
}
public void releasePlayer() {
player.setPlayWhenReady(false);
player.release();
player.stop(true);
}
像这样附加视图时设置 Exoplayer
@Override
public void onViewAttachedToWindow(@NonNull VideoPlayerViewHolder holder) {
super.onViewAttachedToWindow(holder);
holder.setExoplayer();
holder.startPlayer();
}