android – ExoPlayer2 – 如何从片段中释放
作者:互联网
我有一个活动有2个片段(不是ViewPager)&想在其中一个片段中使用ExoPlayer2来播放内容.最初的片段显示了一个内容列表&单击时,显示第二个片段播放所选内容.然而,当我点击回到初始片段时,我仍然可以听到播放的内容;好像第二个片段仍然是开放的.我(尝试)以这种方式释放玩家:
@Override
public void onStop() {
super.onStop();
releasePlayer();
}
public void releasePlayer() {
if (player != null) {
player.setPlayWhenReady(true);
player.stop();
player.release();
player = null;
trackSelector = null;
simpleExoPlayerView.setPlayer(null);
simpleExoPlayerView = null;
System.out.println("releasePlayer");
}
}
我读了this,但没有提供太多的解决方案.另外,阅读几篇在ViewPager片段中使用ExoPlayer的帖子,但不完全确定这些示例是如何发布播放器的.遵循发现here的演示应用程序中的releasePlayer()方法,但没有运气.从onPause(),onDetach(),onDestroyView()调用releasePlayer()但它没有任何区别.
这是在将监听器添加到播放器之后设置的一些日志记录,但是没有看到任何解释为什么它不会释放…
07-05 19:01:07.216 2529-2643/? V/RenderScript: 0xe0494000 Launching thread(s), CPUs 4
07-05 19:01:11.871 3064-3064/? V/MediaCodecInfo: Listener-onPlayerStateChanged...
07-05 19:01:11.876 3064-3064/? V/MediaCodecInfo: Listener-onPlayerStateChanged...
07-05 19:01:11.904 3064-3064/? V/MediaCodecInfo: Listener-onPlayerStateChanged...
07-05 19:01:11.904 3064-3064/? V/MediaCodecInfo: Listener-onTimelineChanged...
07-05 19:01:11.904 3064-3064/? V/MediaCodecInfo: Listener-onLoadingChanged...
07-05 19:01:11.904 3064-3064/? V/MediaCodecInfo: Listener-onPlayerStateChanged...
07-05 19:01:11.904 3064-3064/? V/MediaCodecInfo: Listener-onTimelineChanged...
07-05 19:01:11.904 3064-3064/? V/MediaCodecInfo: Listener-onLoadingChanged...
07-05 19:01:12.467 3064-3064/? V/MediaCodecInfo: Listener-onTimelineChanged...
07-05 19:01:12.468 3064-3064/? V/MediaCodecInfo: Listener-onTimelineChanged...
07-05 19:01:12.515 3064-3099/? V/SoftFFmpegVideo: got OMX_IndexParamPortDefinition, width: 320, height: 180
07-05 19:01:12.708 3064-3064/? V/MediaCodecInfo: Listener-onPlayerStateChanged...
07-05 19:01:12.708 3064-3064/? V/MediaCodecInfo: Listener-onPlayerStateChanged...
07-05 19:01:13.380 3064-3064/? V/MediaCodecInfo: Listener-onLoadingChanged...
07-05 19:01:13.655 3064-3064/? V/MediaCodecInfo: Listener-onLoadingChanged...
07-05 19:01:18.528 3064-3099/? V/SoftFFmpegVideo: ~SoftFFmpegVideo
尝试按钮单击但失败了
fragmentExoBinding.button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
System.out.println("button click");
releasePlayer();
}
});
每次关闭它时都会崩溃(使用背压,家庭或近期)b / c不再是玩家.使用if(player!= null)更新了releasePlayer()方法,但即使按下按钮后仍然可以听到内容播放.
解决方法:
我在Activity中使用ExoPlayer,我使用的是两个步骤. onPause我停止播放:
private SimpleExoPlayer player;
@Override
protected void onPause() {
super.onPause();
// Pause video if it's playing
if (isVideoPlaying()) {
if (player != null)
player.setPlayWhenReady(false);
}
}
并且onDestroy我发布了播放器
@Override
protected void onDestroy() {
releasePlayer();
super.onDestroy();
}
private void releasePlayer() {
if (player != null) {
player.stop();
player.release();
player = null;
}
}
标签:android,android-fragments,exoplayer2-x 来源: https://codeday.me/bug/20190705/1391054.html