From 911869ed9f0489404e21d3325017a64c80da1c65 Mon Sep 17 00:00:00 2001 From: zlzw <583819556@qq.com> Date: Sun, 15 Jan 2023 09:56:51 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E9=80=9A=E8=BF=87=E3=80=90?= =?UTF-8?q?=E4=B8=BA=E4=BD=A0=E6=8E=A8=E8=8D=90=E3=80=91=E6=96=B9=E5=BC=8F?= =?UTF-8?q?=E8=BF=9B=E5=85=A5=E7=9B=B4=E6=92=AD=E9=97=B4=E4=BC=9A=E6=9C=89?= =?UTF-8?q?=E4=B8=8A=E4=B8=AA=E7=9B=B4=E6=92=AD=E9=97=B4=E6=AE=8B=E7=95=99?= =?UTF-8?q?=E7=94=BB=E9=9D=A2=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../live/utils/LiveExoPlayerManager.java | 42 +++++++++++++++---- .../live/views/LivePlayRyViewHolder.java | 23 +++++++++- .../live/views/PortraitLiveManager.java | 1 + 3 files changed, 56 insertions(+), 10 deletions(-) diff --git a/live/src/main/java/com/yunbao/live/utils/LiveExoPlayerManager.java b/live/src/main/java/com/yunbao/live/utils/LiveExoPlayerManager.java index 9947f25be..ae558dcdf 100644 --- a/live/src/main/java/com/yunbao/live/utils/LiveExoPlayerManager.java +++ b/live/src/main/java/com/yunbao/live/utils/LiveExoPlayerManager.java @@ -4,6 +4,8 @@ import android.content.Context; import android.os.Handler; import android.os.Looper; import android.util.Log; +import android.view.ViewGroup; +import android.widget.RelativeLayout; import androidx.annotation.NonNull; @@ -25,8 +27,8 @@ import com.google.android.exoplayer2.video.VideoSize; public class LiveExoPlayerManager { private final int MODEL_PLAY1 = 0;//当前主播放器 private final int MODEL_PLAY2 = 1;//当前子播放器 - private final ExoPlayer player1; - private final ExoPlayer player2; + private ExoPlayer player1; + private ExoPlayer player2; private StyledPlayerView mainView;//渲染视图 private int status = MODEL_PLAY1; private Player.Listener listener; @@ -324,14 +326,19 @@ public class LiveExoPlayerManager { * 是否正在播放 */ public boolean isPlaying() { - return getNowPlayer().isPlaying(); + if (getNowPlayer() != null) { + return getNowPlayer().isPlaying(); + } + return false; } /** * 停止播放 */ public void stop() { - getNowPlayer().stop(); + if (getNowPlayer() != null) { + getNowPlayer().stop(); + } clearUrl(); } @@ -344,8 +351,12 @@ public class LiveExoPlayerManager { public void replay() { Log.i(TAG, "replay: 重载播放"); - getNowPlayer().stop(); - getNextPlayer().stop(); + if (getNowPlayer() != null) { + getNowPlayer().stop(); + } + if (getNextPlayer() != null) { + getNextPlayer().stop(); + } String tmp = url; url = null; startUrl(tmp); @@ -363,8 +374,23 @@ public class LiveExoPlayerManager { */ public void release() { Log.i(TAG, "release: 释放播放器"); - player1.release(); - player2.release(); + if (player1 != null) { + player1.clearVideoSurface(); + player1.release(); + } + if (player2 != null) { + player2.clearVideoSurface(); + player2.release(); + } + player1 = null; + player2 = null; + mainView.setKeepContentOnPlayerReset(false); + mainView.setPlayer(null); + RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) mainView.getLayoutParams(); + layoutParams.height = RelativeLayout.LayoutParams.WRAP_CONTENT; + mainView.setLayoutParams(layoutParams); + mainView.removeAllViews(); + mainView.requestLayout(); handler.removeCallbacks(buffRunnable); } diff --git a/live/src/main/java/com/yunbao/live/views/LivePlayRyViewHolder.java b/live/src/main/java/com/yunbao/live/views/LivePlayRyViewHolder.java index 271bdc723..802f1cd20 100644 --- a/live/src/main/java/com/yunbao/live/views/LivePlayRyViewHolder.java +++ b/live/src/main/java/com/yunbao/live/views/LivePlayRyViewHolder.java @@ -103,8 +103,8 @@ public class LivePlayRyViewHolder extends LiveRoomPlayViewHolder { private boolean mPausedPlay;//是否被动暂停了播放 public int landscape; //1h 2s - public static Context contexts; - public static FrameLayout ry_view; + public Context contexts; + public FrameLayout ry_view; private static final int VIDEO_VERTICAL = 2; private static final int VIDEO_HORIZONTAL = 1; @@ -138,6 +138,7 @@ public class LivePlayRyViewHolder extends LiveRoomPlayViewHolder { @Override public void init() { + Log.i(TAG, "init: 初始化播放器ViewHolder"); EventBus.getDefault().register(this); Bus.getOn(this); mRoot = (ViewGroup) findViewById(R.id.root); @@ -376,6 +377,24 @@ public class LivePlayRyViewHolder extends LiveRoomPlayViewHolder { public void clearFrame() { super.clearFrame(); mPlayer.clearFrame(); + RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) mVideoView.getLayoutParams(); + params.height = ViewGroup.LayoutParams.WRAP_CONTENT; + params.topMargin = mContext.getResources().getDimensionPixelOffset(R.dimen.live_top); + params.addRule(RelativeLayout.ALIGN_TOP); + mVideoView.setLayoutParams(params); + mVideoView.requestLayout(); + + RelativeLayout.LayoutParams params1 = (RelativeLayout.LayoutParams) ry_view.getLayoutParams(); + params1.height = ViewGroup.LayoutParams.WRAP_CONTENT; + params1.topMargin = mContext.getResources().getDimensionPixelOffset(R.dimen.live_top); + params1.addRule(RelativeLayout.ALIGN_TOP); + ry_view.setLayoutParams(params1); + ry_view.requestLayout(); + RelativeLayout.LayoutParams params2 = (RelativeLayout.LayoutParams) mCover.getLayoutParams(); + params2.height = DpUtil.dp2px(270); + params2.topMargin = DpUtil.dp2px(120); + mCover.setLayoutParams(params2); + mCover.requestLayout(); } @Override diff --git a/live/src/main/java/com/yunbao/live/views/PortraitLiveManager.java b/live/src/main/java/com/yunbao/live/views/PortraitLiveManager.java index 0bf2b0b5d..a2d2f7704 100644 --- a/live/src/main/java/com/yunbao/live/views/PortraitLiveManager.java +++ b/live/src/main/java/com/yunbao/live/views/PortraitLiveManager.java @@ -314,6 +314,7 @@ public class PortraitLiveManager implements LivePlayListener, SocketMessageListe ImgLoader.displayBlurLive(mContext, mLiveBean.getAvatar(), liveBack, 400, 600); mask.setVisibility(View.VISIBLE); + mLivePlayViewHolder.clearFrame(); mLivePlayViewHolder.setLiveBean(mLiveBean); mLivePlayViewHolder.setCover(mLiveBean.getAvatar()); mLivePlayViewHolder.setLiveBeanLandscape(mLiveBean.getLandscape());