修复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 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();
}

View File

@ -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--->", "还原916");
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) mVideoView.getLayoutParams();
@ -441,6 +442,10 @@ public class LivePlayRyViewHolder extends LiveRoomPlayViewHolder {
@Override
public void setPkview() {
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();
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--->", "还原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();
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: 播放中");
}
}

View File

@ -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);
}