我正在使用firestorepagingadapter进行我的recyclerview。
当片段关闭时,我试图在onstop,onpasue,ondestroy方法中停止exoplayer,但它没有正常工作,它只停止回收器视图中的最后一个视频,但我不明白这里的主要问题是什么。
@Override
public void onStop() {
super.onStop();
adapter.stopListening();
if (HomeVideoHolder.simpleExoPlayer != null) {
HomeVideoHolder.simpleExoPlayer.stop();
}
}
@Override
public void onDestroy() {
super.onDestroy();
adapter.stopListening();
if (HomeVideoHolder.simpleExoPlayer != null) {
HomeVideoHolder.simpleExoPlayer.stop();
}
}
@Override
public void onPause() {
super.onPause();
adapter.stopListening();
if (HomeVideoHolder.simpleExoPlayer != null) {
HomeVideoHolder.simpleExoPlayer.stop();
}
我在homevideoholder中将simpleexoplayer声明为公共静态。
public static PlayerView videoViewpath;
public static SimpleExoPlayer simpleExoPlayer;
我也尝试停止,释放,设置播放时准备(假),并寻求结束视频停止exoplayer。
但每种方法对我的结果都是一样的。他们只停止最后一段视频。
请给我一个解决这个问题的办法。。。。。。
我的适配器代码。。。
adapter=new FirestorePagingAdapter<HomeClass, HomeVideoHolder>(options) {
@Override
protected void onBindViewHolder(@NonNull HomeVideoHolder holder, int position, @NonNull HomeClass model) {
holder.setVideoView(getActivity(),model.getAd(),model.getpId(),model.getcUid(),model.getUid(),model.getPic(),model.getVideo(),model.getcT());
}
@NonNull
@Override
public HomeVideoHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new HomeVideoHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.home_video_item,parent,false));
}
@Override
protected void onLoadingStateChanged(@NonNull LoadingState state) {
switch (state) {
case LOADING_INITIAL:
// The initial load has
swipeRefreshLayout.setRefreshing(true);
break;
// ...
case LOADING_MORE:
// The adapter has started to load an additional page
vLoadMore.setVisibility(View.VISIBLE);
break;
// ...
case LOADED:
// The previous load (either initial or additional) completed
swipeRefreshLayout.setRefreshing(false);
vLoadMore.setVisibility(View.INVISIBLE);
break;
// ...
case FINISHED:
vLoadMore.setVisibility(View.INVISIBLE);
break;
case ERROR:
// The previous load (either initial or additional) failed. Call
adapter.retry();
swipeRefreshLayout.setRefreshing(true);
// the retry() method in order to retry the load operation.
break;
// ...
}
}
};
我的viewholder代码。。。。
public void setVideoView(Activity activity,String ad, String pId, String cUid, String uid, String pic, String video, String cT) {
final CircleImageView chanelProfile=view.findViewById(R.id.cPicH);
final TextView chanelName=view.findViewById(R.id.cNameH);
final TextView videoCaption=view.findViewById(R.id.vDesH);
final ImageView videoThreeDot=view.findViewById(R.id.threeDotHome);
Pkey=pId;
user=FirebaseAuth.getInstance().getCurrentUser().getUid();
videoCaption.setText(cT);
setPlayer(video);
}
private void setPlayer(String video) {
if (playCode.equals("true")) {
LoadControl loadControl = new DefaultLoadControl();
BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
TrackSelector trackSelector = new DefaultTrackSelector(
new AdaptiveTrackSelection.Factory(bandwidthMeter)
);
simpleExoPlayer = ExoPlayerFactory.newSimpleInstance(
view.getContext(), trackSelector, loadControl
);
DefaultHttpDataSourceFactory factory = new DefaultHttpDataSourceFactory(
"Video"
);
ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();
MediaSource mediaSource = new ExtractorMediaSource(Uri.parse(Video),
factory, extractorsFactory, null, null
);
videoViewpath.setPlayer(simpleExoPlayer);
videoViewpath.setKeepScreenOn(true);
simpleExoPlayer.prepare(mediaSource);
simpleExoPlayer.setPlayWhenReady(false);
simpleExoPlayer.addListener(new Player.DefaultEventListener() {
@Override
public void onTimelineChanged(Timeline timeline, Object manifest, int reason) {
super.onTimelineChanged(timeline, manifest, reason);
}
@Override
public void onTracksChanged(TrackGroupArray trackGroups, TrackSelectionArray trackSelections) {
super.onTracksChanged(trackGroups, trackSelections);
}
@Override
public void onLoadingChanged(boolean isLoading) {
super.onLoadingChanged(isLoading);
}
@Override
public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
super.onPlayerStateChanged(playWhenReady, playbackState);
switch (playbackState) {
case Player.STATE_BUFFERING:
progressBar.setVisibility(View.VISIBLE);
break;
case Player.STATE_ENDED:
break;
case Player.STATE_IDLE:
break;
case Player.STATE_READY:
addViwes();
progressBar.setVisibility(View.INVISIBLE);
break;
default:
break;
}
}
@Override
public void onRepeatModeChanged(int repeatMode) {
super.onRepeatModeChanged(repeatMode);
}
@Override
public void onShuffleModeEnabledChanged(boolean shuffleModeEnabled) {
super.onShuffleModeEnabledChanged(shuffleModeEnabled);
}
@Override
public void onPlayerError(ExoPlaybackException error) {
super.onPlayerError(error);
// simpleExoPlayer.prepare(mediaSource);
// simpleExoPlayer.setPlayWhenReady(false);
}
@Override
public void onPositionDiscontinuity(int reason) {
super.onPositionDiscontinuity(reason);
}
@Override
public void onPlaybackParametersChanged(PlaybackParameters playbackParameters) {
super.onPlaybackParametersChanged(playbackParameters);
}
@Override
public void onSeekProcessed() {
super.onSeekProcessed();
}
});
}else {
simpleExoPlayer.stop();
simpleExoPlayer.release();
simpleExoPlayer.clearVideoSurface();
videoViewpath.setPlayer(null);
}
}
1条答案
按热度按时间6yt4nkrj1#
不申报
PlayerView
以及SimpleExoPlayer
作为static
,这是错误的方法。。。您只保留对最后一个视频播放器示例的引用,并且希望所有示例都暂停/停止。每HomeViewHolder
应该只保留自己的玩家参考,static
字段在引用保持类的所有示例中只保留一个示例(“所有者”,HomeViewHolder
)删除所有
static
玩家停止代码onPause
,onStop
以及onDestroy
,播放器示例(非-static
)无论如何都无法访问覆盖
onViewDetachedFromWindow(HomeVideoHolder holder)
和/或onViewRecycled(HomeVideoHolder holder)
方法,在此位置暂停/停止连接到单个HomeVideoHolder
让适配器停止每一个球员时,单个项目得到回收/分离,而不仅仅是销毁Activity
/Fragment
. 目前,你可能有一些内存泄漏,因为启动的播放器可能留在内存中播放一些视频,你没有它的参考(只有最后一个,static
字段)以释放资源。。。当你摧毁RecyclerView
然后连接到它的适配器将回收/销毁所有当前存在的HomeViewPager
s。检查此模式(回收视图)如何与一些Log
呼叫,作为舒尔,您可以recyclerView.setAdapter(null)
在onDestroy
(之前)super
电话)