修复PK时画面异常问题(分辨率未切到4:3)

优化播放器加载速度2
This commit is contained in:
zlzw 2023-01-08 17:17:31 +08:00
parent 27bd65d3da
commit 182e46799a
3 changed files with 56 additions and 9 deletions

View File

@ -6,6 +6,7 @@ import android.os.Looper;
import android.util.Log; import android.util.Log;
import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.DefaultLoadControl;
import com.google.android.exoplayer2.ExoPlayer; import com.google.android.exoplayer2.ExoPlayer;
import com.google.android.exoplayer2.MediaItem; import com.google.android.exoplayer2.MediaItem;
import com.google.android.exoplayer2.Player; 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.AspectRatioFrameLayout;
import com.google.android.exoplayer2.ui.StyledPlayerView; import com.google.android.exoplayer2.ui.StyledPlayerView;
import com.google.android.exoplayer2.video.VideoSize; 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 String TAG = "播放器";
private int playBufferIndex = 0;//卡顿计数器 private int playBufferIndex = 0;//卡顿计数器
private Handler handler; private Handler handler;
private static double log_buffer_time = 0, log_buffer_max_time;
public LiveExoPlayerManager(Context mContext) { public LiveExoPlayerManager(Context mContext) {
this.mContext = mContext; this.mContext = mContext;
player1 = new ExoPlayer.Builder(mContext).build(); DefaultLoadControl control = new DefaultLoadControl.Builder()
player2 = new ExoPlayer.Builder(mContext).build(); .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); player1.setVideoScalingMode(C.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING);
player2.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()); handler = new Handler(Looper.getMainLooper());
setListener(); setListener();
setAnalyticsListener(); setAnalyticsListener();
@ -51,7 +65,9 @@ public class LiveExoPlayerManager {
this.mainView = mainView; this.mainView = mainView;
this.mainView.setKeepContentOnPlayerReset(true); this.mainView.setKeepContentOnPlayerReset(true);
} }
public void setViewResizeMode(boolean isPhone) { public void setViewResizeMode(boolean isPhone) {
Log.i(TAG, "setViewResizeMode: "+isPhone);
mainView.setResizeMode(isPhone ? AspectRatioFrameLayout.RESIZE_MODE_ZOOM : AspectRatioFrameLayout.RESIZE_MODE_FIT); mainView.setResizeMode(isPhone ? AspectRatioFrameLayout.RESIZE_MODE_ZOOM : AspectRatioFrameLayout.RESIZE_MODE_FIT);
mainView.requestLayout(); mainView.requestLayout();
} }
@ -126,6 +142,14 @@ public class LiveExoPlayerManager {
if (isPlaying) { if (isPlaying) {
Log.i(TAG, "onIsPlayingChanged1: 播放了"); Log.i(TAG, "onIsPlayingChanged1: 播放了");
//player1.setVideoSurfaceView(mainView); //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); mainView.setPlayer(player1);
status = MODEL_PLAY1; status = MODEL_PLAY1;
isSwitchUrl = false; isSwitchUrl = false;
@ -180,6 +204,12 @@ public class LiveExoPlayerManager {
Log.i(TAG, "onIsPlayingChanged2: 播放了"); Log.i(TAG, "onIsPlayingChanged2: 播放了");
//player2.setVideoSurfaceView(mainView); //player2.setVideoSurfaceView(mainView);
mainView.setPlayer(player2); 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; status = MODEL_PLAY2;
isSwitchUrl = false; isSwitchUrl = false;
if (listener != null) { if (listener != null) {
@ -214,6 +244,7 @@ public class LiveExoPlayerManager {
Log.i(TAG, "startUrl: " + url + " > " + mainView.getResizeMode()); Log.i(TAG, "startUrl: " + url + " > " + mainView.getResizeMode());
isSwitchUrl = true; isSwitchUrl = true;
playBufferIndex = 0; playBufferIndex = 0;
log_buffer_time = System.currentTimeMillis();
//getNowPlayer().setVideoSurfaceView(mainView); //getNowPlayer().setVideoSurfaceView(mainView);
mainView.setPlayer(getNowPlayer()); mainView.setPlayer(getNowPlayer());
getNowPlayer().setMediaItem(createMediaItem(url)); getNowPlayer().setMediaItem(createMediaItem(url));
@ -230,6 +261,7 @@ public class LiveExoPlayerManager {
Log.i(TAG, "switchUrl: " + url); Log.i(TAG, "switchUrl: " + url);
playBufferIndex = 0; playBufferIndex = 0;
isSwitchUrl = true; isSwitchUrl = true;
log_buffer_time = System.currentTimeMillis();
getNextPlayer().setMediaItem(createMediaItem(url)); getNextPlayer().setMediaItem(createMediaItem(url));
getNextPlayer().prepare(); getNextPlayer().prepare();
} }

View File

@ -391,6 +391,7 @@ public class LivePlayRyViewHolder extends LiveRoomPlayViewHolder {
} else { } else {
height = DpUtil.dp2px(285); height = DpUtil.dp2px(285);
} }
mPlayer.setViewResizeMode(landscape == VIDEO_VERTICAL);
if (landscape == 2) { if (landscape == 2) {
Log.i("收到socket--->", "还原916"); Log.i("收到socket--->", "还原916");
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) mVideoView.getLayoutParams(); RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) mVideoView.getLayoutParams();
@ -441,6 +442,10 @@ public class LivePlayRyViewHolder extends LiveRoomPlayViewHolder {
@Override @Override
public void setPkview() { public void setPkview() {
Log.i("收到socket--->", "变成169"); Log.i("收到socket--->", "变成169");
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(); RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) mVideoView.getLayoutParams();
params.height = vHeight; params.height = vHeight;
params.topMargin = mContext.getResources().getDimensionPixelOffset(R.dimen.live_top); params.topMargin = mContext.getResources().getDimensionPixelOffset(R.dimen.live_top);
@ -455,12 +460,22 @@ public class LivePlayRyViewHolder extends LiveRoomPlayViewHolder {
if (landscape == 2) { if (landscape == 2) {
Log.i("收到socket--->", "还原916"); Log.i("收到socket--->", "还原916");
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(); RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) mVideoView.getLayoutParams();
params.height = RelativeLayout.LayoutParams.MATCH_PARENT; params.height = RelativeLayout.LayoutParams.MATCH_PARENT;
params.topMargin = 0; params.topMargin = 0;
mVideoView.requestLayout(); mVideoView.requestLayout();
} else { } 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(); RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) mVideoView.getLayoutParams();
params.height = vHeight; params.height = vHeight;
params.topMargin = mContext.getResources().getDimensionPixelOffset(R.dimen.live_top); params.topMargin = mContext.getResources().getDimensionPixelOffset(R.dimen.live_top);

View File

@ -586,7 +586,7 @@ public class PortraitLiveManager implements LivePlayListener, SocketMessageListe
mLiveRoomViewHolder.showPrizePoolLevel(String.valueOf(giftPrizePoolLevel)); mLiveRoomViewHolder.showPrizePoolLevel(String.valueOf(giftPrizePoolLevel));
} }
} }
//liveHandler.postDelayed(() -> loading.setVisibility(View.GONE), 700); liveHandler.postDelayed(() -> loading.setVisibility(View.GONE), 5_000);
} }