From b3083cdd1874841ae5af7f5562b477087fa3bf35 Mon Sep 17 00:00:00 2001 From: Yutousama <583819556@qq.com> Date: Sun, 8 Jan 2023 21:49:07 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E4=BF=9D=E5=AD=98=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E9=80=89=E6=8B=A9=E5=88=86=E8=BE=A8=E7=8E=87=E7=9A=84?= =?UTF-8?q?=E8=AE=BE=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../live/utils/LiveExoPlayerManager.java | 36 +++++++++---------- .../live/views/LivePlayRyViewHolder.java | 36 +++++++++++++------ .../live/views/LiveRoomPlayViewHolder.java | 12 +++++++ .../live/views/PortraitLiveManager.java | 13 +++++-- 4 files changed, 66 insertions(+), 31 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 114473fc2..6601dca54 100644 --- a/live/src/main/java/com/yunbao/live/utils/LiveExoPlayerManager.java +++ b/live/src/main/java/com/yunbao/live/utils/LiveExoPlayerManager.java @@ -14,9 +14,8 @@ import com.google.android.exoplayer2.analytics.AnalyticsListener; import com.google.android.exoplayer2.ui.AspectRatioFrameLayout; import com.google.android.exoplayer2.ui.StyledPlayerView; import com.google.android.exoplayer2.video.VideoSize; -import com.yunbao.common.utils.ToastUtil; -import java.util.Locale; +import androidx.annotation.NonNull; /** * 直播间播放器管理器 @@ -25,19 +24,18 @@ import java.util.Locale; public class LiveExoPlayerManager { private final int MODEL_PLAY1 = 0;//当前主播放器 private final int MODEL_PLAY2 = 1;//当前子播放器 - private Context mContext; - private ExoPlayer player1, player2; + private final ExoPlayer player1; + private final ExoPlayer player2; private StyledPlayerView mainView;//渲染视图 private int status = MODEL_PLAY1; private Player.Listener listener; private boolean isSwitchUrl = false;//是否为主动切换播放器 - private String TAG = "播放器"; + private final String TAG = "播放器"; private int playBufferIndex = 0;//卡顿计数器 - private Handler handler; + private final Handler handler; private static double log_buffer_time = 0, log_buffer_max_time; public LiveExoPlayerManager(Context mContext) { - this.mContext = mContext; DefaultLoadControl control = new DefaultLoadControl.Builder() .setPrioritizeTimeOverSizeThresholds(false) .setBackBuffer(15_000, true) @@ -46,8 +44,8 @@ public class LiveExoPlayerManager { 50, 100) .build(); - player1 = new ExoPlayer.Builder(mContext).setLoadControl(control).build(); - player2 = new ExoPlayer.Builder(mContext).setLoadControl(control).build(); + player1 = new ExoPlayer.Builder(mContext).build(); + player2 = new ExoPlayer.Builder(mContext).build(); player1.setVideoScalingMode(C.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING); player2.setVideoScalingMode(C.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING); @@ -76,13 +74,13 @@ public class LiveExoPlayerManager { player1.addAnalyticsListener(new AnalyticsListener() { @Override public void onPlaybackStateChanged( - EventTime eventTime, int state) { + @NonNull EventTime eventTime, int state) { Log.d(TAG, "onPlaybackStateChanged(1) called with: eventTime = [" + eventTime + "], state = [" + state + "]"); } @Override public void onDroppedVideoFrames( - EventTime eventTime, int droppedFrames, long elapsedMs) { + @NonNull EventTime eventTime, int droppedFrames, long elapsedMs) { Log.d(TAG, "onDroppedVideoFrames(1) called with: eventTime = [" + eventTime + "], droppedFrames = [" + droppedFrames + "], elapsedMs = [" + elapsedMs + "]"); } }); @@ -90,13 +88,13 @@ public class LiveExoPlayerManager { player2.addAnalyticsListener(new AnalyticsListener() { @Override public void onPlaybackStateChanged( - EventTime eventTime, int state) { + @NonNull EventTime eventTime, int state) { Log.d(TAG, "onPlaybackStateChanged(2) called with: eventTime = [" + eventTime.totalBufferedDurationMs + "], state = [" + state + "]"); } @Override public void onDroppedVideoFrames( - EventTime eventTime, int droppedFrames, long elapsedMs) { + @NonNull EventTime eventTime, int droppedFrames, long elapsedMs) { Log.d(TAG, "onDroppedVideoFrames(2) called with: eventTime = [" + eventTime + "], droppedFrames = [" + droppedFrames + "], elapsedMs = [" + elapsedMs + "]"); } }); @@ -106,7 +104,7 @@ public class LiveExoPlayerManager { /** * 延迟1秒还未恢复播放则认为卡顿了,可以切分辨率了 */ - private Runnable buffRunnable = new Runnable() { + private final Runnable buffRunnable = new Runnable() { @Override public void run() { playBufferIndex = 0; @@ -147,7 +145,7 @@ public class LiveExoPlayerManager { if (tmp > log_buffer_max_time) { log_buffer_max_time = tmp; } - ToastUtil.show(String.format(Locale.CHINA, "从加载到播放 = %.3f,最大耗时 = %.3f", tmp, log_buffer_max_time)); + //ToastUtil.show(String.format(Locale.CHINA, "从加载到播放 = %.3f,最大耗时 = %.3f", tmp, log_buffer_max_time)); log_buffer_time = -1; } mainView.setPlayer(player1); @@ -160,7 +158,7 @@ public class LiveExoPlayerManager { } @Override - public void onVideoSizeChanged(VideoSize videoSize) { + public void onVideoSizeChanged(@NonNull VideoSize videoSize) { Player.Listener.super.onVideoSizeChanged(videoSize); setViewResizeMode(videoSize.height > videoSize.width); Log.i(TAG, "onVideoSizeChanged: width = " + videoSize.width + " height = " + videoSize.height); @@ -208,7 +206,7 @@ public class LiveExoPlayerManager { if (tmp > log_buffer_max_time) { log_buffer_max_time = tmp; } - ToastUtil.show(String.format(Locale.CHINA, "从加载到播放 = %.3f,最大耗时 = %.3f", tmp, log_buffer_max_time)); + //ToastUtil.show(String.format(Locale.CHINA, "从加载到播放 = %.3f,最大耗时 = %.3f", tmp, log_buffer_max_time)); log_buffer_time = -1; status = MODEL_PLAY2; isSwitchUrl = false; @@ -219,7 +217,7 @@ public class LiveExoPlayerManager { } @Override - public void onVideoSizeChanged(VideoSize videoSize) { + public void onVideoSizeChanged(@NonNull VideoSize videoSize) { Player.Listener.super.onVideoSizeChanged(videoSize); setViewResizeMode(videoSize.height > videoSize.width); if (listener != null) { @@ -246,7 +244,9 @@ public class LiveExoPlayerManager { playBufferIndex = 0; log_buffer_time = System.currentTimeMillis(); //getNowPlayer().setVideoSurfaceView(mainView); + mainView.setKeepContentOnPlayerReset(false); mainView.setPlayer(getNowPlayer()); + mainView.setKeepContentOnPlayerReset(true); getNowPlayer().setMediaItem(createMediaItem(url)); getNowPlayer().prepare(); getNowPlayer().play(); 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 828767f4d..aac9da7cd 100644 --- a/live/src/main/java/com/yunbao/live/views/LivePlayRyViewHolder.java +++ b/live/src/main/java/com/yunbao/live/views/LivePlayRyViewHolder.java @@ -32,6 +32,7 @@ import com.lzf.easyfloat.EasyFloat; import com.lzy.okserver.OkDownload; import com.yunbao.common.http.HttpCallback; import com.yunbao.common.http.HttpClient; +import com.yunbao.common.manager.IMLoginManager; import com.yunbao.common.utils.Bus; import com.yunbao.common.utils.DialogUitl; import com.yunbao.common.utils.DpUtil; @@ -442,8 +443,10 @@ public class LivePlayRyViewHolder extends LiveRoomPlayViewHolder { @Override public void setPkview() { Log.i("收到socket--->", "变成16:9"); - String url=srcUrl; - url = url.replace(".flv", videoRatioHorizontal[PLAY_MODEL] + videoFps[0] + ".flv"); + String url = srcUrl; + if(PLAY_MODEL!=-1) { + url = url.replace(".flv", videoRatioHorizontal[PLAY_MODEL] + videoFps[0] + ".flv"); + } mPlayer.setViewResizeMode(false); mPlayer.switchUrl(url); RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) mVideoView.getLayoutParams(); @@ -460,8 +463,10 @@ public class LivePlayRyViewHolder extends LiveRoomPlayViewHolder { if (landscape == 2) { Log.i("收到socket--->", "还原9:16"); - String url=srcUrl; - url = url.replace(".flv", videoRatioVertical[PLAY_MODEL] + videoFps[0] + ".flv"); + String url = srcUrl; + if (PLAY_MODEL != -1) { + url = url.replace(".flv", videoRatioVertical[PLAY_MODEL] + videoFps[0] + ".flv"); + } mPlayer.setViewResizeMode(true); mPlayer.switchUrl(url); @@ -471,8 +476,10 @@ public class LivePlayRyViewHolder extends LiveRoomPlayViewHolder { mVideoView.requestLayout(); } else { - String url=srcUrl; - url = url.replace(".flv", videoRatioHorizontal[PLAY_MODEL] + videoFps[0] + ".flv"); + String url = srcUrl; + if(PLAY_MODEL!=-1) { + url = url.replace(".flv", videoRatioHorizontal[PLAY_MODEL] + videoFps[0] + ".flv"); + } mPlayer.setViewResizeMode(false); mPlayer.switchUrl(url); @@ -719,7 +726,10 @@ public class LivePlayRyViewHolder extends LiveRoomPlayViewHolder { break; } - LiveClarityCustomPopup liveClarityCustomPopup = new LiveClarityCustomPopup(mContext, PLAY_MODEL, ban, true); + LiveClarityCustomPopup liveClarityCustomPopup = new LiveClarityCustomPopup(mContext, + IMLoginManager.get(mContext).getInt(PLAY_MODEL_KEY, PLAY_MODEL), + ban, + true); new XPopup.Builder(mContext) .setPopupCallback(new XPopupCallback() { @Override @@ -740,8 +750,14 @@ public class LivePlayRyViewHolder extends LiveRoomPlayViewHolder { @Override public void onDismiss(BasePopupView popupView) { int selectClarity = liveClarityCustomPopup.getSelectClarity(); - if (selectClarity == PLAY_MODEL || PLAY_MODEL == PLAY_MODEL_DEF) return; - switchStream(srcUrl, selectClarity); + if (selectClarity == PLAY_MODEL) return; + if (selectClarity == PLAY_MODEL_ANCHOR) { + switchStream(srcUrl, PLAY_MODEL_DEF); + setAudiencePlayModel(selectClarity); + } else { + switchStream(srcUrl, selectClarity); + } + IMLoginManager.get(mContext).put(LiveRoomPlayViewHolder.PLAY_MODEL_KEY, selectClarity); showToast(); } @@ -1087,7 +1103,7 @@ public class LivePlayRyViewHolder extends LiveRoomPlayViewHolder { public void onVideoSizeChanged(VideoSize videoSize) { Player.Listener.super.onVideoSizeChanged(videoSize); Log.i(TAG, "onVideoSizeChanged: width = " + videoSize.width + " height = " + videoSize.height); - // ToastUtil.show("分辨率: width = " + videoSize.width + " height = " + videoSize.height); + ToastUtil.show("分辨率: 宽 = " + videoSize.width + " 高 = " + videoSize.height); if (videoSize.height > videoSize.width) { videoLandscape = VIDEO_VERTICAL; } else { diff --git a/live/src/main/java/com/yunbao/live/views/LiveRoomPlayViewHolder.java b/live/src/main/java/com/yunbao/live/views/LiveRoomPlayViewHolder.java index 3a6fa1bc9..6aab89439 100644 --- a/live/src/main/java/com/yunbao/live/views/LiveRoomPlayViewHolder.java +++ b/live/src/main/java/com/yunbao/live/views/LiveRoomPlayViewHolder.java @@ -5,6 +5,7 @@ import android.view.ViewGroup; import com.yunbao.common.bean.EnterRoomNewModel; import com.yunbao.common.bean.LiveBean; +import com.yunbao.common.utils.ToastUtil; import com.yunbao.common.views.AbsViewHolder; import com.yunbao.live.interfaces.ILiveLinkMicViewHolder; @@ -14,10 +15,12 @@ import com.yunbao.live.interfaces.ILiveLinkMicViewHolder; public abstract class LiveRoomPlayViewHolder extends AbsViewHolder implements ILiveLinkMicViewHolder { protected LiveBean mLiveBean; + public static final String PLAY_MODEL_KEY = "user_audience_play_model"; public static final int PLAY_MODEL_DEF = -1; public static final int PLAY_MODEL_480 = 0; public static final int PLAY_MODEL_720 = 1; public static final int PLAY_MODEL_1080 = 2; + public static int PLAY_MODEL_ANCHOR = -1;//主播设置的最高画质 public static int PLAY_MODEL = PLAY_MODEL_DEF; public static final String[] videoRatioHorizontal = new String[]{"_640_480", "_960_720", "_1440_1080"}; @@ -31,6 +34,15 @@ public abstract class LiveRoomPlayViewHolder extends AbsViewHolder implements IL super(context, parentView); } + public void setAnchorPlayModel(int model) { + PLAY_MODEL_ANCHOR = model; + } + + public void setAudiencePlayModel(int model) { + PLAY_MODEL = model; + } + + public abstract void play(String url, int playModel); public abstract void switchStream(String url, int playModel); 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 a57abcb1d..205c5ba47 100644 --- a/live/src/main/java/com/yunbao/live/views/PortraitLiveManager.java +++ b/live/src/main/java/com/yunbao/live/views/PortraitLiveManager.java @@ -306,7 +306,7 @@ public class PortraitLiveManager implements LivePlayListener, SocketMessageListe mLivePlayViewHolder.setLiveBean(mLiveBean); mLivePlayViewHolder.setCover(mLiveBean.getAvatar()); mLivePlayViewHolder.setLiveBeanLandscape(mLiveBean.getLandscape()); - mLivePlayViewHolder.play(mLiveBean.getPull(), LiveRoomPlayViewHolder.PLAY_MODEL_DEF); + //mLivePlayViewHolder.play(mLiveBean.getPull(), LiveRoomPlayViewHolder.PLAY_MODEL_DEF); mLivePlayViewHolder.setOnMicCallback(new LiveRoomPlayViewHolder.OnMicCallback() { @Override public void onMikUpdate() { @@ -364,7 +364,14 @@ public class PortraitLiveManager implements LivePlayListener, SocketMessageListe landscape = data.getLiveInfo().getLandscape(); mLivePlayViewHolder.setLiveBeanLandscape(landscape); mLivePlayViewHolder.setLiveEnterRoomNewModel(data); - //mLivePlayViewHolder.switchStream(mLiveBean.getPull(), (data.getClarityType() - 1) == 2 ? 1 : (data.getClarityType() - 1)); + int userClarityType = IMLoginManager.get(mContext).getInt(LiveRoomPlayViewHolder.PLAY_MODEL_KEY, -1); + int anchorClarityType = data.getClarityType() - 1; + if (userClarityType != anchorClarityType) { + mLivePlayViewHolder.play(mLiveBean.getPull(), userClarityType); + } else { + mLivePlayViewHolder.play(mLiveBean.getPull(), LiveRoomPlayViewHolder.PLAY_MODEL_DEF); + } + mLivePlayViewHolder.setAnchorPlayModel(anchorClarityType); //是否热度卡加成 liveHandler.postDelayed(() -> mLiveRoomViewHolder.getIsHot(data.getIsUseHotCard()), 700); @@ -586,7 +593,7 @@ public class PortraitLiveManager implements LivePlayListener, SocketMessageListe mLiveRoomViewHolder.showPrizePoolLevel(String.valueOf(giftPrizePoolLevel)); } } - liveHandler.postDelayed(() -> loading.setVisibility(View.GONE), 5_000); + liveHandler.postDelayed(() -> loading.setVisibility(View.GONE), 10_000); }