update 清晰度切换

This commit is contained in:
zlzw 2022-12-29 16:16:08 +08:00
parent 660853d5be
commit 7555510d94
2 changed files with 43 additions and 114 deletions

View File

@ -14,17 +14,21 @@ import com.google.android.exoplayer2.analytics.PlaybackStats;
import com.google.android.exoplayer2.analytics.PlaybackStatsListener; import com.google.android.exoplayer2.analytics.PlaybackStatsListener;
import com.google.android.exoplayer2.video.VideoSize; import com.google.android.exoplayer2.video.VideoSize;
/**
* 直播间播放器管理器
* 通过预加载子播放器来实现无缝切换分辨率功能
*/
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 Context mContext;
private ExoPlayer player1, player2; private ExoPlayer player1, player2;
private SurfaceView mainView; private SurfaceView 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 String TAG = "播放";
private int playBufferIndex = 0; private int playBufferIndex = 0;//卡顿计数器
private Handler handler; private Handler handler;
public LiveExoPlayerManager(Context mContext) { public LiveExoPlayerManager(Context mContext) {
@ -75,6 +79,9 @@ public class LiveExoPlayerManager {
} }
/**
* 延迟1秒还未恢复播放则认为卡顿了可以切分辨率了
*/
private Runnable buffRunnable = new Runnable() { private Runnable buffRunnable = new Runnable() {
@Override @Override
public void run() { public void run() {
@ -185,6 +192,11 @@ public class LiveExoPlayerManager {
}); });
} }
/**
* 开始播放
*
* @param url 地址
*/
public void startUrl(String url) { public void startUrl(String url) {
isSwitchUrl = true; isSwitchUrl = true;
playBufferIndex = 0; playBufferIndex = 0;
@ -194,6 +206,11 @@ public class LiveExoPlayerManager {
getNowPlayer().play(); getNowPlayer().play();
} }
/**
* 无缝切换
*
* @param url 地址
*/
public void switchUrl(String url) { public void switchUrl(String url) {
Log.i(TAG, "switchUrl: " + url); Log.i(TAG, "switchUrl: " + url);
playBufferIndex = 0; playBufferIndex = 0;
@ -206,26 +223,44 @@ public class LiveExoPlayerManager {
return MediaItem.fromUri(url); return MediaItem.fromUri(url);
} }
/**
* 获取当前播放器
*/
public ExoPlayer getNowPlayer() { public ExoPlayer getNowPlayer() {
return status == MODEL_PLAY1 ? player1 : player2; return status == MODEL_PLAY1 ? player1 : player2;
} }
/**
* 获取下一个播放器
*/
private ExoPlayer getNextPlayer() { private ExoPlayer getNextPlayer() {
return status == MODEL_PLAY1 ? player2 : player1; return status == MODEL_PLAY1 ? player2 : player1;
} }
/**
* 是否正在播放
*/
public boolean isPlaying() { public boolean isPlaying() {
return getNowPlayer().isPlaying(); return getNowPlayer().isPlaying();
} }
/**
* 停止播放
*/
public void stop() { public void stop() {
getNowPlayer().stop(); getNowPlayer().stop();
} }
/**
* 开始播放
*/
public void play() { public void play() {
getNowPlayer().play(); getNowPlayer().play();
} }
/**
* 释放播放器
*/
public void release() { public void release() {
player1.release(); player1.release();
player2.release(); player2.release();

View File

@ -34,6 +34,7 @@ import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.BasePopupView; import com.lxj.xpopup.core.BasePopupView;
import com.lxj.xpopup.interfaces.XPopupCallback; import com.lxj.xpopup.interfaces.XPopupCallback;
import com.lzf.easyfloat.EasyFloat; import com.lzf.easyfloat.EasyFloat;
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.utils.Bus; import com.yunbao.common.utils.Bus;
@ -284,118 +285,9 @@ public class LivePlayRyViewHolder extends LiveRoomPlayViewHolder {
return; return;
} }
if (TextUtils.isEmpty(url) || mVideoView == null) { if (TextUtils.isEmpty(url) || mVideoView == null) {
return; return;
} }
/*mPlayer.setObserver(new V2TXLivePlayerObserver() {
@Override
public void onWarning(V2TXLivePlayer player, int code, String msg, Bundle extraInfo) {
super.onWarning(player, code, msg, extraInfo);
Log.i(TAG, "onWarning: " + "player = " + player + ", code = " + code + ", msg = " + msg + ", extraInfo = " + extraInfo);
if (code == 2105) {
// mPlayer.resumeVideo();
// mPlayer.resumeAudio();
mPlayer.stopPlay();
mPlayer.startPlay(purl);
}
debugView.setText("播放警告code=" + code + " msg=" + msg);
}
@Override
public void onVideoPlayStatusUpdate(V2TXLivePlayer player, V2TXLiveDef.V2TXLivePlayStatus status, V2TXLiveDef.V2TXLiveStatusChangeReason reason, Bundle extraInfo) {
super.onVideoPlayStatusUpdate(player, status, reason, extraInfo);
//Log.i(TAG, "onVideoPlayStatusUpdate: " + "player = " + player + ", status = " + status + ", reason = " + reason + ", extraInfo = " + extraInfo);
}
@Override
public void onAudioPlayStatusUpdate(V2TXLivePlayer player, V2TXLiveDef.V2TXLivePlayStatus status, V2TXLiveDef.V2TXLiveStatusChangeReason reason, Bundle extraInfo) {
super.onAudioPlayStatusUpdate(player, status, reason, extraInfo);
//Log.i(TAG, "onAudioPlayStatusUpdate: " + "player = " + player + ", status = " + status + ", reason = " + reason + ", extraInfo = " + extraInfo);
}
@Override
public void onPlayoutVolumeUpdate(V2TXLivePlayer player, int volume) {
super.onPlayoutVolumeUpdate(player, volume);
}
@Override
public void onStatisticsUpdate(V2TXLivePlayer player, V2TXLiveDef.V2TXLivePlayerStatistics statistics) {
super.onStatisticsUpdate(player, statistics);
Map<String, DownloadTask> taskMap = OkDownload.getInstance().getTaskMap();
StringBuilder buffer = new StringBuilder();
buffer.append("|");
for (String key : taskMap.keySet()) {
DownloadTask task = taskMap.get(key);
buffer.append(task.progress.fileName).append(":");
int status = task.progress.status;
switch (status) {
case 0:
buffer.append("无状态");
break;
case 1:
buffer.append("等待");
break;
case 2:
buffer.append("下载中:").append(task.progress.fraction);
break;
case 3:
buffer.append("暂停");
break;
case 4:
buffer.append("错误");
break;
case 5:
buffer.append("完成");
break;
}
buffer.append("|");
}
hideCover();
String debugText = "视频码率:" + statistics.videoBitrate + "\n" +
"音频码率:" + statistics.audioBitrate + "\n" +
"FPS:" + statistics.fps + "\n" +
"CPU:" + statistics.appCpu + "\n" +
"剩余内存:" + DeviceUtils.getMemory(mContext) + "\n" +
"分辨率:" + statistics.height + "x" + statistics.width + "\n" +
"播放状态:" + mPlayer.isPlaying() + "\n" +
"运行时间:" + (System.currentTimeMillis() - CrashSaveBean.getInstance().getStartTime()) / 1000 + "\n";
debugView.setText(debugText);
Log.i(TAG, "onStatisticsUpdate: " + JSON.toJSONString(statistics) + " |当前下载数 : " + OkDownload.getInstance().getTaskMap().size() + buffer);
if (statistics.height > statistics.width) {
videoLandscape = VIDEO_VERTICAL;
} else {
videoLandscape = VIDEO_HORIZONTAL;
}
if (statistics.fps == 0) {
OkDownload.getInstance().pauseAll();
} else {
OkDownload.getInstance().startAll();
}
}
@Override
public void onSnapshotComplete(V2TXLivePlayer player, Bitmap image) {
super.onSnapshotComplete(player, image);
}
@Override
public void onRenderVideoFrame(V2TXLivePlayer player, V2TXLiveDef.V2TXLiveVideoFrame videoFrame) {
super.onRenderVideoFrame(player, videoFrame);
}
@Override
public void onReceiveSeiMessage(V2TXLivePlayer player, int payloadType, byte[] data) {
super.onReceiveSeiMessage(player, payloadType, data);
}
});*/
if (mPlayer.isPlaying()) { if (mPlayer.isPlaying()) {
mPlayer.stop(); mPlayer.stop();
} }
@ -1187,6 +1079,7 @@ public class LivePlayRyViewHolder extends LiveRoomPlayViewHolder {
Player.Listener.super.onPlaybackStateChanged(playbackState); Player.Listener.super.onPlaybackStateChanged(playbackState);
if (playbackState == Player.STATE_BUFFERING) { if (playbackState == Player.STATE_BUFFERING) {
showLoadingDialog(); showLoadingDialog();
OkDownload.getInstance().pauseAll();
} }
} }
@ -1195,6 +1088,7 @@ public class LivePlayRyViewHolder extends LiveRoomPlayViewHolder {
Player.Listener.super.onIsPlayingChanged(isPlaying); Player.Listener.super.onIsPlayingChanged(isPlaying);
if (isPlaying) { if (isPlaying) {
hideCover(); hideCover();
OkDownload.getInstance().startAll();
} }
} }
} }