在视图页的滑动上停止 Exoplayer
我正在使用一个带有单个片段的实例,其中我显示图像,视频,音频等文件。ViewPager
Media
我已经实现了处理和文件。对于图像。ExoPlayer
Video
Audio
Glide
为了避免内存泄漏,我正在释放对象,如下所示:ExoPlayer
ItemViewerFragment.java
private void releasePlayer() {
if (player != null) {
player.release();
player = null;
trackSelector = null;
simpleExoPlayerView.setPlayer(null);
}
}
@Override
public void onStart() {
super.onStart();
if (player == null && currentGalleryModel != null) {
initializePlayer();
}
}
@Override
public void onResume() {
super.onResume();
if (player == null && currentGalleryModel != null) {
initializePlayer();
}
}
@Override
public void onPause() {
super.onPause();
releasePlayer();
}
@Override
public void onStop() {
super.onStop();
releasePlayer();
}
在我初始化视图时,如下所示:onViewCreated()
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
if (currentGalleryModel.isVideo() || currentGalleryModel.isAudio()) {
simpleExoPlayerView.setVisibility(View.VISIBLE);
imageView.setVisibility(View.GONE);
initializePlayer();
} else if (currentGalleryModel.isImage() || currentGalleryModel.isGif()) {
simpleExoPlayerView.setVisibility(View.GONE);
imageView.setVisibility(View.VISIBLE);
Glide.with(this)
.load(currentGalleryModel.getFilePath())
.placeholder(android.R.color.black)
.fitCenter()
.diskCacheStrategy(DiskCacheStrategy.NONE)
.into(imageView);
}
}
我正在使用.这是方法:FragmentStatePagerAdapeter
getItem
@Override
public Fragment getItem(int position) {
return ItemViewerFragment.newInstance(mItems.get(position));
}
我无法在第一次滑动时检测到片段。第二次滑动时,文件将停止播放。onPause
Viewpager
video/audio
在活动中,我尝试添加:.addOnPageChangeListener
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
try {
((ItemViewerFragment)mAdapter.getItem(mPreviousPos)).imHiddenNow();
} catch (Exception e) {
e.printStackTrace();
}
mPreviousPos = position;
}
在 :ItemViewerFragment.java
public void imHiddenNow(){
releasePlayer();
}
仍然继续玩。video/audio
这是截屏视频的视频链接。