From 182e46799adef8ae86515d258efdbb1e76006682 Mon Sep 17 00:00:00 2001 From: zlzw <583819556@qq.com> Date: Sun, 8 Jan 2023 17:17:31 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8DPK=E6=97=B6=E7=94=BB=E9=9D=A2?= =?UTF-8?q?=E5=BC=82=E5=B8=B8=E9=97=AE=E9=A2=98=EF=BC=88=E5=88=86=E8=BE=A8?= =?UTF-8?q?=E7=8E=87=E6=9C=AA=E5=88=87=E5=88=B04:3=EF=BC=89=20=E4=BC=98?= =?UTF-8?q?=E5=8C=96=E6=92=AD=E6=94=BE=E5=99=A8=E5=8A=A0=E8=BD=BD=E9=80=9F?= =?UTF-8?q?=E5=BA=A62?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../live/utils/LiveExoPlayerManager.java | 46 ++++++++++++++++--- .../live/views/LivePlayRyViewHolder.java | 17 ++++++- .../live/views/PortraitLiveManager.java | 2 +- 3 files changed, 56 insertions(+), 9 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 76dd81632..7a406fcd0 100644 --- a/live/src/main/java/com/yunbao/live/utils/LiveExoPlayerManager.java +++ b/live/src/main/java/com/yunbao/live/utils/LiveExoPlayerManager.java @@ -6,6 +6,7 @@ import android.os.Looper; import android.util.Log; import com.google.android.exoplayer2.C; +import com.google.android.exoplayer2.DefaultLoadControl; import com.google.android.exoplayer2.ExoPlayer; import com.google.android.exoplayer2.MediaItem; import com.google.android.exoplayer2.Player; @@ -13,6 +14,9 @@ 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; /** * 直播间播放器管理器 @@ -30,13 +34,23 @@ public class LiveExoPlayerManager { private String TAG = "播放器"; private int playBufferIndex = 0;//卡顿计数器 private Handler handler; + private static double log_buffer_time = 0, log_buffer_max_time; public LiveExoPlayerManager(Context mContext) { this.mContext = mContext; - player1 = new ExoPlayer.Builder(mContext).build(); - player2 = new ExoPlayer.Builder(mContext).build(); + DefaultLoadControl control = new DefaultLoadControl.Builder() + .setPrioritizeTimeOverSizeThresholds(true) + .setBackBuffer(15_000, true) + .setBufferDurationsMs(100, + 15_000, + 50, + 100) + .build(); + player1 = new ExoPlayer.Builder(mContext).setLoadControl(control).build(); + player2 = new ExoPlayer.Builder(mContext).setLoadControl(control).build(); player1.setVideoScalingMode(C.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING); player2.setVideoScalingMode(C.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING); + handler = new Handler(Looper.getMainLooper()); setListener(); setAnalyticsListener(); @@ -51,8 +65,10 @@ public class LiveExoPlayerManager { this.mainView = mainView; this.mainView.setKeepContentOnPlayerReset(true); } - public void setViewResizeMode(boolean isPhone){ - mainView.setResizeMode(isPhone?AspectRatioFrameLayout.RESIZE_MODE_ZOOM:AspectRatioFrameLayout.RESIZE_MODE_FIT); + + public void setViewResizeMode(boolean isPhone) { + Log.i(TAG, "setViewResizeMode: "+isPhone); + mainView.setResizeMode(isPhone ? AspectRatioFrameLayout.RESIZE_MODE_ZOOM : AspectRatioFrameLayout.RESIZE_MODE_FIT); mainView.requestLayout(); } @@ -126,6 +142,14 @@ public class LiveExoPlayerManager { if (isPlaying) { Log.i(TAG, "onIsPlayingChanged1: 播放了"); //player1.setVideoSurfaceView(mainView); + if (log_buffer_time != -1) { + double tmp = (System.currentTimeMillis() - log_buffer_time) / 1000; + if (tmp > log_buffer_max_time) { + log_buffer_max_time = tmp; + } + ToastUtil.show(String.format(Locale.CHINA, "从加载到播放 = %.3f,最大耗时 = %.3f", tmp, log_buffer_max_time)); + log_buffer_time = -1; + } mainView.setPlayer(player1); status = MODEL_PLAY1; isSwitchUrl = false; @@ -138,7 +162,7 @@ public class LiveExoPlayerManager { @Override public void onVideoSizeChanged(VideoSize videoSize) { Player.Listener.super.onVideoSizeChanged(videoSize); - setViewResizeMode(videoSize.height>videoSize.width); + setViewResizeMode(videoSize.height > videoSize.width); Log.i(TAG, "onVideoSizeChanged: width = " + videoSize.width + " height = " + videoSize.height); if (listener != null) { listener.onVideoSizeChanged(videoSize); @@ -180,6 +204,12 @@ public class LiveExoPlayerManager { Log.i(TAG, "onIsPlayingChanged2: 播放了"); //player2.setVideoSurfaceView(mainView); mainView.setPlayer(player2); + double tmp = (System.currentTimeMillis() - log_buffer_time) / 1000; + if (tmp > log_buffer_max_time) { + log_buffer_max_time = tmp; + } + ToastUtil.show(String.format(Locale.CHINA, "从加载到播放 = %.3f,最大耗时 = %.3f", tmp, log_buffer_max_time)); + log_buffer_time = -1; status = MODEL_PLAY2; isSwitchUrl = false; if (listener != null) { @@ -191,7 +221,7 @@ public class LiveExoPlayerManager { @Override public void onVideoSizeChanged(VideoSize videoSize) { Player.Listener.super.onVideoSizeChanged(videoSize); - setViewResizeMode(videoSize.height>videoSize.width); + setViewResizeMode(videoSize.height > videoSize.width); if (listener != null) { listener.onVideoSizeChanged(videoSize); } @@ -211,9 +241,10 @@ public class LiveExoPlayerManager { * @param url 地址 */ public void startUrl(String url) { - Log.i(TAG, "startUrl: "+url+" > "+mainView.getResizeMode()); + Log.i(TAG, "startUrl: " + url + " > " + mainView.getResizeMode()); isSwitchUrl = true; playBufferIndex = 0; + log_buffer_time = System.currentTimeMillis(); //getNowPlayer().setVideoSurfaceView(mainView); mainView.setPlayer(getNowPlayer()); getNowPlayer().setMediaItem(createMediaItem(url)); @@ -230,6 +261,7 @@ public class LiveExoPlayerManager { Log.i(TAG, "switchUrl: " + url); playBufferIndex = 0; isSwitchUrl = true; + log_buffer_time = System.currentTimeMillis(); getNextPlayer().setMediaItem(createMediaItem(url)); getNextPlayer().prepare(); } 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 6d1d43753..828767f4d 100644 --- a/live/src/main/java/com/yunbao/live/views/LivePlayRyViewHolder.java +++ b/live/src/main/java/com/yunbao/live/views/LivePlayRyViewHolder.java @@ -391,6 +391,7 @@ public class LivePlayRyViewHolder extends LiveRoomPlayViewHolder { } else { height = DpUtil.dp2px(285); } + mPlayer.setViewResizeMode(landscape == VIDEO_VERTICAL); if (landscape == 2) { Log.i("收到socket--->", "还原9:16"); RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) mVideoView.getLayoutParams(); @@ -441,6 +442,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"); + mPlayer.setViewResizeMode(false); + mPlayer.switchUrl(url); RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) mVideoView.getLayoutParams(); params.height = vHeight; params.topMargin = mContext.getResources().getDimensionPixelOffset(R.dimen.live_top); @@ -455,12 +460,22 @@ 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"); + mPlayer.setViewResizeMode(true); + mPlayer.switchUrl(url); + RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) mVideoView.getLayoutParams(); params.height = RelativeLayout.LayoutParams.MATCH_PARENT; params.topMargin = 0; mVideoView.requestLayout(); } else { + String url=srcUrl; + url = url.replace(".flv", videoRatioHorizontal[PLAY_MODEL] + videoFps[0] + ".flv"); + mPlayer.setViewResizeMode(false); + mPlayer.switchUrl(url); + RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) mVideoView.getLayoutParams(); params.height = vHeight; params.topMargin = mContext.getResources().getDimensionPixelOffset(R.dimen.live_top); @@ -1087,7 +1102,7 @@ public class LivePlayRyViewHolder extends LiveRoomPlayViewHolder { //showLoadingDialog(); OkDownload.getInstance().pauseAll(); Log.i(TAG, "onPlaybackStateChanged: 缓存中"); - }else{ + } else { Log.i(TAG, "onPlaybackStateChanged: 播放中"); } } 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 a973827e4..8530ba7c8 100644 --- a/live/src/main/java/com/yunbao/live/views/PortraitLiveManager.java +++ b/live/src/main/java/com/yunbao/live/views/PortraitLiveManager.java @@ -586,7 +586,7 @@ public class PortraitLiveManager implements LivePlayListener, SocketMessageListe mLiveRoomViewHolder.showPrizePoolLevel(String.valueOf(giftPrizePoolLevel)); } } - //liveHandler.postDelayed(() -> loading.setVisibility(View.GONE), 700); + liveHandler.postDelayed(() -> loading.setVisibility(View.GONE), 5_000); }