新增保存用户选择分辨率的设置

This commit is contained in:
Yutousama 2023-01-08 21:49:07 +08:00
parent cf85870810
commit b3083cdd18
4 changed files with 66 additions and 31 deletions

View File

@ -14,9 +14,8 @@ 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; import androidx.annotation.NonNull;
/** /**
* 直播间播放器管理器 * 直播间播放器管理器
@ -25,19 +24,18 @@ import java.util.Locale;
public class LiveExoPlayerManager { public class LiveExoPlayerManager {
private final int MODEL_PLAY1 = 0;//当前主播放器 private final int MODEL_PLAY1 = 0;//当前主播放器
private final int MODEL_PLAY2 = 1;//当前子播放器 private final int MODEL_PLAY2 = 1;//当前子播放器
private Context mContext; private final ExoPlayer player1;
private ExoPlayer player1, player2; private final ExoPlayer player2;
private StyledPlayerView mainView;//渲染视图 private StyledPlayerView mainView;//渲染视图
private int status = MODEL_PLAY1; private int status = MODEL_PLAY1;
private Player.Listener listener; private Player.Listener listener;
private boolean isSwitchUrl = false;//是否为主动切换播放器 private boolean isSwitchUrl = false;//是否为主动切换播放器
private String TAG = "播放器"; private final String TAG = "播放器";
private int playBufferIndex = 0;//卡顿计数器 private int playBufferIndex = 0;//卡顿计数器
private Handler handler; private final Handler handler;
private static double log_buffer_time = 0, log_buffer_max_time; private static double log_buffer_time = 0, log_buffer_max_time;
public LiveExoPlayerManager(Context mContext) { public LiveExoPlayerManager(Context mContext) {
this.mContext = mContext;
DefaultLoadControl control = new DefaultLoadControl.Builder() DefaultLoadControl control = new DefaultLoadControl.Builder()
.setPrioritizeTimeOverSizeThresholds(false) .setPrioritizeTimeOverSizeThresholds(false)
.setBackBuffer(15_000, true) .setBackBuffer(15_000, true)
@ -46,8 +44,8 @@ public class LiveExoPlayerManager {
50, 50,
100) 100)
.build(); .build();
player1 = new ExoPlayer.Builder(mContext).setLoadControl(control).build(); player1 = new ExoPlayer.Builder(mContext).build();
player2 = new ExoPlayer.Builder(mContext).setLoadControl(control).build(); player2 = new ExoPlayer.Builder(mContext).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);
@ -76,13 +74,13 @@ public class LiveExoPlayerManager {
player1.addAnalyticsListener(new AnalyticsListener() { player1.addAnalyticsListener(new AnalyticsListener() {
@Override @Override
public void onPlaybackStateChanged( public void onPlaybackStateChanged(
EventTime eventTime, int state) { @NonNull EventTime eventTime, int state) {
Log.d(TAG, "onPlaybackStateChanged(1) called with: eventTime = [" + eventTime + "], state = [" + state + "]"); Log.d(TAG, "onPlaybackStateChanged(1) called with: eventTime = [" + eventTime + "], state = [" + state + "]");
} }
@Override @Override
public void onDroppedVideoFrames( 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 + "]"); Log.d(TAG, "onDroppedVideoFrames(1) called with: eventTime = [" + eventTime + "], droppedFrames = [" + droppedFrames + "], elapsedMs = [" + elapsedMs + "]");
} }
}); });
@ -90,13 +88,13 @@ public class LiveExoPlayerManager {
player2.addAnalyticsListener(new AnalyticsListener() { player2.addAnalyticsListener(new AnalyticsListener() {
@Override @Override
public void onPlaybackStateChanged( public void onPlaybackStateChanged(
EventTime eventTime, int state) { @NonNull EventTime eventTime, int state) {
Log.d(TAG, "onPlaybackStateChanged(2) called with: eventTime = [" + eventTime.totalBufferedDurationMs + "], state = [" + state + "]"); Log.d(TAG, "onPlaybackStateChanged(2) called with: eventTime = [" + eventTime.totalBufferedDurationMs + "], state = [" + state + "]");
} }
@Override @Override
public void onDroppedVideoFrames( 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 + "]"); Log.d(TAG, "onDroppedVideoFrames(2) called with: eventTime = [" + eventTime + "], droppedFrames = [" + droppedFrames + "], elapsedMs = [" + elapsedMs + "]");
} }
}); });
@ -106,7 +104,7 @@ public class LiveExoPlayerManager {
/** /**
* 延迟1秒还未恢复播放则认为卡顿了可以切分辨率了 * 延迟1秒还未恢复播放则认为卡顿了可以切分辨率了
*/ */
private Runnable buffRunnable = new Runnable() { private final Runnable buffRunnable = new Runnable() {
@Override @Override
public void run() { public void run() {
playBufferIndex = 0; playBufferIndex = 0;
@ -147,7 +145,7 @@ public class LiveExoPlayerManager {
if (tmp > log_buffer_max_time) { if (tmp > log_buffer_max_time) {
log_buffer_max_time = tmp; 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; log_buffer_time = -1;
} }
mainView.setPlayer(player1); mainView.setPlayer(player1);
@ -160,7 +158,7 @@ public class LiveExoPlayerManager {
} }
@Override @Override
public void onVideoSizeChanged(VideoSize videoSize) { public void onVideoSizeChanged(@NonNull VideoSize videoSize) {
Player.Listener.super.onVideoSizeChanged(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); Log.i(TAG, "onVideoSizeChanged: width = " + videoSize.width + " height = " + videoSize.height);
@ -208,7 +206,7 @@ public class LiveExoPlayerManager {
if (tmp > log_buffer_max_time) { if (tmp > log_buffer_max_time) {
log_buffer_max_time = tmp; 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; log_buffer_time = -1;
status = MODEL_PLAY2; status = MODEL_PLAY2;
isSwitchUrl = false; isSwitchUrl = false;
@ -219,7 +217,7 @@ public class LiveExoPlayerManager {
} }
@Override @Override
public void onVideoSizeChanged(VideoSize videoSize) { public void onVideoSizeChanged(@NonNull VideoSize videoSize) {
Player.Listener.super.onVideoSizeChanged(videoSize); Player.Listener.super.onVideoSizeChanged(videoSize);
setViewResizeMode(videoSize.height > videoSize.width); setViewResizeMode(videoSize.height > videoSize.width);
if (listener != null) { if (listener != null) {
@ -246,7 +244,9 @@ public class LiveExoPlayerManager {
playBufferIndex = 0; playBufferIndex = 0;
log_buffer_time = System.currentTimeMillis(); log_buffer_time = System.currentTimeMillis();
//getNowPlayer().setVideoSurfaceView(mainView); //getNowPlayer().setVideoSurfaceView(mainView);
mainView.setKeepContentOnPlayerReset(false);
mainView.setPlayer(getNowPlayer()); mainView.setPlayer(getNowPlayer());
mainView.setKeepContentOnPlayerReset(true);
getNowPlayer().setMediaItem(createMediaItem(url)); getNowPlayer().setMediaItem(createMediaItem(url));
getNowPlayer().prepare(); getNowPlayer().prepare();
getNowPlayer().play(); getNowPlayer().play();

View File

@ -32,6 +32,7 @@ import com.lzf.easyfloat.EasyFloat;
import com.lzy.okserver.OkDownload; import com.lzy.okserver.OkDownload;
import com.yunbao.common.http.HttpCallback; import com.yunbao.common.http.HttpCallback;
import com.yunbao.common.http.HttpClient; import com.yunbao.common.http.HttpClient;
import com.yunbao.common.manager.IMLoginManager;
import com.yunbao.common.utils.Bus; import com.yunbao.common.utils.Bus;
import com.yunbao.common.utils.DialogUitl; import com.yunbao.common.utils.DialogUitl;
import com.yunbao.common.utils.DpUtil; import com.yunbao.common.utils.DpUtil;
@ -442,8 +443,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; String url = srcUrl;
if(PLAY_MODEL!=-1) {
url = url.replace(".flv", videoRatioHorizontal[PLAY_MODEL] + videoFps[0] + ".flv"); url = url.replace(".flv", videoRatioHorizontal[PLAY_MODEL] + videoFps[0] + ".flv");
}
mPlayer.setViewResizeMode(false); mPlayer.setViewResizeMode(false);
mPlayer.switchUrl(url); mPlayer.switchUrl(url);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) mVideoView.getLayoutParams(); RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) mVideoView.getLayoutParams();
@ -460,8 +463,10 @@ public class LivePlayRyViewHolder extends LiveRoomPlayViewHolder {
if (landscape == 2) { if (landscape == 2) {
Log.i("收到socket--->", "还原916"); Log.i("收到socket--->", "还原916");
String url=srcUrl; String url = srcUrl;
if (PLAY_MODEL != -1) {
url = url.replace(".flv", videoRatioVertical[PLAY_MODEL] + videoFps[0] + ".flv"); url = url.replace(".flv", videoRatioVertical[PLAY_MODEL] + videoFps[0] + ".flv");
}
mPlayer.setViewResizeMode(true); mPlayer.setViewResizeMode(true);
mPlayer.switchUrl(url); mPlayer.switchUrl(url);
@ -471,8 +476,10 @@ public class LivePlayRyViewHolder extends LiveRoomPlayViewHolder {
mVideoView.requestLayout(); mVideoView.requestLayout();
} else { } else {
String url=srcUrl; String url = srcUrl;
if(PLAY_MODEL!=-1) {
url = url.replace(".flv", videoRatioHorizontal[PLAY_MODEL] + videoFps[0] + ".flv"); url = url.replace(".flv", videoRatioHorizontal[PLAY_MODEL] + videoFps[0] + ".flv");
}
mPlayer.setViewResizeMode(false); mPlayer.setViewResizeMode(false);
mPlayer.switchUrl(url); mPlayer.switchUrl(url);
@ -719,7 +726,10 @@ public class LivePlayRyViewHolder extends LiveRoomPlayViewHolder {
break; 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) new XPopup.Builder(mContext)
.setPopupCallback(new XPopupCallback() { .setPopupCallback(new XPopupCallback() {
@Override @Override
@ -740,8 +750,14 @@ public class LivePlayRyViewHolder extends LiveRoomPlayViewHolder {
@Override @Override
public void onDismiss(BasePopupView popupView) { public void onDismiss(BasePopupView popupView) {
int selectClarity = liveClarityCustomPopup.getSelectClarity(); int selectClarity = liveClarityCustomPopup.getSelectClarity();
if (selectClarity == PLAY_MODEL || PLAY_MODEL == PLAY_MODEL_DEF) return; if (selectClarity == PLAY_MODEL) return;
if (selectClarity == PLAY_MODEL_ANCHOR) {
switchStream(srcUrl, PLAY_MODEL_DEF);
setAudiencePlayModel(selectClarity);
} else {
switchStream(srcUrl, selectClarity); switchStream(srcUrl, selectClarity);
}
IMLoginManager.get(mContext).put(LiveRoomPlayViewHolder.PLAY_MODEL_KEY, selectClarity);
showToast(); showToast();
} }
@ -1087,7 +1103,7 @@ public class LivePlayRyViewHolder extends LiveRoomPlayViewHolder {
public void onVideoSizeChanged(VideoSize videoSize) { public void onVideoSizeChanged(VideoSize videoSize) {
Player.Listener.super.onVideoSizeChanged(videoSize); Player.Listener.super.onVideoSizeChanged(videoSize);
Log.i(TAG, "onVideoSizeChanged: width = " + videoSize.width + " height = " + videoSize.height); 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) { if (videoSize.height > videoSize.width) {
videoLandscape = VIDEO_VERTICAL; videoLandscape = VIDEO_VERTICAL;
} else { } else {

View File

@ -5,6 +5,7 @@ import android.view.ViewGroup;
import com.yunbao.common.bean.EnterRoomNewModel; import com.yunbao.common.bean.EnterRoomNewModel;
import com.yunbao.common.bean.LiveBean; import com.yunbao.common.bean.LiveBean;
import com.yunbao.common.utils.ToastUtil;
import com.yunbao.common.views.AbsViewHolder; import com.yunbao.common.views.AbsViewHolder;
import com.yunbao.live.interfaces.ILiveLinkMicViewHolder; import com.yunbao.live.interfaces.ILiveLinkMicViewHolder;
@ -14,10 +15,12 @@ import com.yunbao.live.interfaces.ILiveLinkMicViewHolder;
public abstract class LiveRoomPlayViewHolder extends AbsViewHolder implements ILiveLinkMicViewHolder { public abstract class LiveRoomPlayViewHolder extends AbsViewHolder implements ILiveLinkMicViewHolder {
protected LiveBean mLiveBean; 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_DEF = -1;
public static final int PLAY_MODEL_480 = 0; public static final int PLAY_MODEL_480 = 0;
public static final int PLAY_MODEL_720 = 1; public static final int PLAY_MODEL_720 = 1;
public static final int PLAY_MODEL_1080 = 2; 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 int PLAY_MODEL = PLAY_MODEL_DEF;
public static final String[] videoRatioHorizontal = new String[]{"_640_480", "_960_720", "_1440_1080"}; 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); 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 play(String url, int playModel);
public abstract void switchStream(String url, int playModel); public abstract void switchStream(String url, int playModel);

View File

@ -306,7 +306,7 @@ public class PortraitLiveManager implements LivePlayListener, SocketMessageListe
mLivePlayViewHolder.setLiveBean(mLiveBean); mLivePlayViewHolder.setLiveBean(mLiveBean);
mLivePlayViewHolder.setCover(mLiveBean.getAvatar()); mLivePlayViewHolder.setCover(mLiveBean.getAvatar());
mLivePlayViewHolder.setLiveBeanLandscape(mLiveBean.getLandscape()); 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() { mLivePlayViewHolder.setOnMicCallback(new LiveRoomPlayViewHolder.OnMicCallback() {
@Override @Override
public void onMikUpdate() { public void onMikUpdate() {
@ -364,7 +364,14 @@ public class PortraitLiveManager implements LivePlayListener, SocketMessageListe
landscape = data.getLiveInfo().getLandscape(); landscape = data.getLiveInfo().getLandscape();
mLivePlayViewHolder.setLiveBeanLandscape(landscape); mLivePlayViewHolder.setLiveBeanLandscape(landscape);
mLivePlayViewHolder.setLiveEnterRoomNewModel(data); 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); liveHandler.postDelayed(() -> mLiveRoomViewHolder.getIsHot(data.getIsUseHotCard()), 700);
@ -586,7 +593,7 @@ public class PortraitLiveManager implements LivePlayListener, SocketMessageListe
mLiveRoomViewHolder.showPrizePoolLevel(String.valueOf(giftPrizePoolLevel)); mLiveRoomViewHolder.showPrizePoolLevel(String.valueOf(giftPrizePoolLevel));
} }
} }
liveHandler.postDelayed(() -> loading.setVisibility(View.GONE), 5_000); liveHandler.postDelayed(() -> loading.setVisibility(View.GONE), 10_000);
} }