update 清晰度切换

This commit is contained in:
zlzw 2022-12-29 11:01:23 +08:00
parent 137dbc72d8
commit 181bb5b445
4 changed files with 227 additions and 87 deletions

View File

@ -494,8 +494,12 @@ public class DialogUitl {
}
}
};
btnConfirm.setOnClickListener(listener);
btnCancel.setOnClickListener(listener);
if(btnConfirm!=null) {
btnConfirm.setOnClickListener(listener);
}
if(btnCancel!=null) {
btnCancel.setOnClickListener(listener);
}
dialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialogInterface) {

View File

@ -0,0 +1,170 @@
package com.yunbao.live.utils;
import android.content.Context;
import android.util.Log;
import android.view.SurfaceView;
import com.google.android.exoplayer2.ExoPlayer;
import com.google.android.exoplayer2.MediaItem;
import com.google.android.exoplayer2.Player;
import com.google.android.exoplayer2.video.VideoSize;
public class LiveExoPlayerManager {
private final int MODEL_PLAY1 = 0;
private final int MODEL_PLAY2 = 1;
private Context mContext;
private ExoPlayer player1, player2;
private SurfaceView mainView;
private int status = MODEL_PLAY1;
private Player.Listener listener;
private boolean isSwitchUrl = false;
private String TAG = "播放";
public LiveExoPlayerManager(Context mContext) {
this.mContext = mContext;
player1 = new ExoPlayer.Builder(mContext).build();
player2 = new ExoPlayer.Builder(mContext).build();
player1.addListener(new Player.Listener() {
@Override
public void onPlaybackStateChanged(int playbackState) {
Player.Listener.super.onPlaybackStateChanged(playbackState);
Log.i(TAG, "onPlaybackStateChanged 1: " + playbackState);
if (playbackState == Player.STATE_READY) {
player2.stop();
player2.setVideoSurface(null);
player1.play();
Log.i(TAG, "切换播放器1");
} else if (playbackState == Player.STATE_BUFFERING && status == MODEL_PLAY1 && !isSwitchUrl) {
if (listener != null) {
listener.onPlaybackStateChanged(playbackState);
}
}
}
@Override
public void onIsPlayingChanged(boolean isPlaying) {
Player.Listener.super.onIsPlayingChanged(isPlaying);
if (isPlaying) {
Log.i(TAG, "onIsPlayingChanged1: 播放了");
player1.setVideoSurfaceView(mainView);
status = MODEL_PLAY1;
isSwitchUrl = false;
if (listener != null) {
listener.onIsPlayingChanged(true);
}
}
}
@Override
public void onVideoSizeChanged(VideoSize videoSize) {
Player.Listener.super.onVideoSizeChanged(videoSize);
if (listener != null) {
listener.onVideoSizeChanged(videoSize);
}
}
@Override
public void onIsLoadingChanged(boolean isLoading) {
Player.Listener.super.onIsLoadingChanged(isLoading);
Log.i(TAG, "onIsLoadingChanged: 1 " + isLoading);
}
});
player2.addListener(new Player.Listener() {
@Override
public void onPlaybackStateChanged(int playbackState) {
Player.Listener.super.onPlaybackStateChanged(playbackState);
Log.i(TAG, "onPlaybackStateChanged 2: " + playbackState);
if (playbackState == Player.STATE_READY) {
player1.stop();
player1.setVideoSurface(null);
player2.play();
Log.i(TAG, "切换播放器2 " + player2.isPlaying());
} else if (playbackState == Player.STATE_BUFFERING && status == MODEL_PLAY2 && !isSwitchUrl) {
if (listener != null) {
listener.onPlaybackStateChanged(playbackState);
}
}
}
@Override
public void onIsPlayingChanged(boolean isPlaying) {
Player.Listener.super.onIsPlayingChanged(isPlaying);
if (isPlaying) {
Log.i(TAG, "onIsPlayingChanged2: 播放了");
player2.setVideoSurfaceView(mainView);
status = MODEL_PLAY2;
isSwitchUrl = false;
if (listener != null) {
listener.onIsPlayingChanged(true);
}
}
}
@Override
public void onVideoSizeChanged(VideoSize videoSize) {
Player.Listener.super.onVideoSizeChanged(videoSize);
if (listener != null) {
listener.onVideoSizeChanged(videoSize);
}
}
@Override
public void onIsLoadingChanged(boolean isLoading) {
Player.Listener.super.onIsLoadingChanged(isLoading);
Log.i(TAG, "onIsLoadingChanged: 2 " + isLoading);
}
});
}
public void setListener(Player.Listener listener) {
this.listener = listener;
}
public void setMainView(SurfaceView mainView) {
this.mainView = mainView;
}
public void startUrl(String url) {
isSwitchUrl = true;
getNowPlayer().setVideoSurfaceView(mainView);
getNowPlayer().setMediaItem(createMediaItem(url));
getNowPlayer().prepare();
getNowPlayer().play();
}
public void switchUrl(String url) {
isSwitchUrl = true;
getNextPlayer().setMediaItem(createMediaItem(url));
getNextPlayer().prepare();
}
private MediaItem createMediaItem(String url) {
return MediaItem.fromUri(url);
}
public ExoPlayer getNowPlayer() {
return status == MODEL_PLAY1 ? player1 : player2;
}
private ExoPlayer getNextPlayer() {
return status == MODEL_PLAY1 ? player2 : player1;
}
public boolean isPlaying() {
return getNowPlayer().isPlaying();
}
public void stop() {
getNowPlayer().stop();
}
public void play() {
getNowPlayer().play();
}
public void release() {
player1.release();
player2.release();
}
}

View File

@ -24,7 +24,6 @@ import android.widget.TextView;
import androidx.annotation.Nullable;
import com.google.android.exoplayer2.ExoPlayer;
import com.google.android.exoplayer2.MediaItem;
import com.google.android.exoplayer2.PlaybackException;
import com.google.android.exoplayer2.Player;
@ -44,6 +43,7 @@ import com.yunbao.common.utils.WordUtil;
import com.yunbao.live.R;
import com.yunbao.live.activity.LiveActivity;
import com.yunbao.live.activity.LiveAudienceActivity;
import com.yunbao.live.utils.LiveExoPlayerManager;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
@ -106,13 +106,11 @@ public class LivePlayRyViewHolder extends LiveRoomPlayViewHolder {
static int vHeight;//视频高
private TextView debugView;
private ExoPlayer mPlayer, mPlayer2;
private boolean isPlayer = true;
private LiveExoPlayerManager mPlayer;
//0未申请1申请中2连麦中
RCRTCRoom rcrtcRoom;
String purl;
String purl, srcUrl;
public int getLandscape() {
return landscape;
@ -153,13 +151,9 @@ public class LivePlayRyViewHolder extends LiveRoomPlayViewHolder {
params.height = vHeight;
mPkContainer.requestLayout();
mPlayer = new ExoPlayer.Builder(mContext)
.build();
mPlayer2 = new ExoPlayer.Builder(mContext).build();
mPlayer.setVideoSurfaceView(mVideoView);
mPlayer2.setVideoSurfaceView(mVideoView);
mPlayer.addListener(new ExoPlayerListener(true));
mPlayer2.addListener(new ExoPlayerListener(false));
mPlayer = new LiveExoPlayerManager(mContext);
mPlayer.setMainView(mVideoView);
mPlayer.setListener(new ExoPlayerListener());
debugView = new TextView(mContext);
debugView.setBackgroundColor(Color.WHITE);
@ -267,6 +261,8 @@ public class LivePlayRyViewHolder extends LiveRoomPlayViewHolder {
*/
@Override
public void play(String url, int playModel) {
srcUrl = url;
PLAY_MODEL = playModel;
if (playModel != PLAY_MODEL_DEF) {
if (videoLandscape == VIDEO_VERTICAL) {
url = url.replace(".flv", videoRatioVertical[playModel] + videoFps[0] + ".flv");
@ -395,16 +391,10 @@ public class LivePlayRyViewHolder extends LiveRoomPlayViewHolder {
MediaItem item = createMediaItem(url);
if (mPlayer.isPlaying()) {
ToastUtil.show("set 2");
mPlayer2.setMediaItem(item);
mPlayer2.setVideoSurfaceView(mVideoView);
mPlayer2.prepare();
mPlayer2.play();
mPlayer.switchUrl(url);
} else {
ToastUtil.show("set 1");
mPlayer.setMediaItem(item);
mPlayer.setVideoSurfaceView(mVideoView);
mPlayer.prepare();
mPlayer.play();
mPlayer.startUrl(url);
}
/* if (mPlayer.isPlaying()) {
MediaSource nextSource = new ProgressiveMediaSource.Factory(new DefaultHttpDataSource.Factory())
@ -743,10 +733,7 @@ public class LivePlayRyViewHolder extends LiveRoomPlayViewHolder {
mPlayer.play();
Log.e("ry", mPlayer.isPlaying() + "purl" + purl);
if (!mPlayer.isPlaying()) {
MediaItem item = createMediaItem(purl);
mPlayer.setMediaItem(item);
mPlayer.prepare();
mPlayer.play();
mPlayer.switchUrl(purl);
}
ry_view.removeAllViews();
ry_view.getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT;
@ -1061,58 +1048,31 @@ public class LivePlayRyViewHolder extends LiveRoomPlayViewHolder {
});
}
protected void startPlay() {
if (mPlayer.isPlaying()) {
mPlayer.stop();
mPlayer2.play();
} else {
mPlayer2.stop();
mPlayer.play();
private void showLoadingDialog() {
if (PLAY_MODEL == PLAY_MODEL_480) return;
new DialogUitl.Builder(mContext)
.setTitle("網絡提示")
.setContent("系統監測到您的網絡不穩定,對此將清晰度變成了流暢,您可以在側邊菜單中的「清晰度」選擇調回。")
.setView(R.layout.dialog_simple_tip)
.setClickCallback(new DialogUitl.SimpleCallback() {
@Override
public void onConfirmClick(Dialog dialog, String content) {
dialog.dismiss();
}
}).build().show();
PLAY_MODEL = PLAY_MODEL_480;
String url = srcUrl;
if (videoLandscape == VIDEO_VERTICAL) {
url = url.replace(".flv", videoRatioVertical[PLAY_MODEL_480] + videoFps[0] + ".flv");
} else if (videoLandscape == VIDEO_HORIZONTAL) {
url = url.replace(".flv", videoRatioHorizontal[PLAY_MODEL_480] + videoFps[0] + ".flv");
}
mPlayer.switchUrl(url);
}
private class ExoPlayerListener implements Player.Listener {
String TAG = "播放流";
boolean isPlayer1;
public ExoPlayerListener(boolean isPlayer1) {
this.isPlayer1 = isPlayer1;
}
@Override
public void onPlaybackStateChanged(int playbackState) {
Player.Listener.super.onPlaybackStateChanged(playbackState);
if (playbackState == Player.STATE_READY) {
if (isPlayer1) {
mPlayer2.stop();
Log.i(TAG, "onPlaybackStateChanged1: " + mPlayer.getCurrentMediaItem().localConfiguration.uri);
mPlayer.setVideoSurfaceView(mVideoView);
mPlayer.play();
} else {
mPlayer.stop();
Log.i(TAG, "onPlaybackStateChanged2: " + mPlayer2.getCurrentMediaItem().localConfiguration.uri);
mPlayer2.setVideoSurfaceView(mVideoView);
mPlayer2.play();
}
}else if (playbackState==Player.STATE_IDLE){
if(!isPlayer1){
mPlayer.stop();
mPlayer2.prepare();
}else{
/* mPlayer2.stop();
mPlayer.prepare();*/
}
}
Log.i(TAG, "onPlaybackStateChanged: " + playbackState + " play = " + isPlayer1 + " p1 = " + mPlayer.isPlaying() + " p2 = " + mPlayer2.isPlaying());
isPlayer = !isPlayer;
}
@Override
public void onMediaItemTransition(@Nullable MediaItem mediaItem, int reason) {
Player.Listener.super.onMediaItemTransition(mediaItem, reason);
Log.i(TAG, "onMediaItemTransition: " + reason);
}
@Override
public void onPlayerError(PlaybackException error) {
@ -1123,6 +1083,7 @@ public class LivePlayRyViewHolder extends LiveRoomPlayViewHolder {
@Override
public void onVideoSizeChanged(VideoSize videoSize) {
Player.Listener.super.onVideoSizeChanged(videoSize);
Log.i(TAG, "onVideoSizeChanged: width = " + videoSize.width + " height = " + videoSize.height);
if (videoSize.height > videoSize.width) {
videoLandscape = VIDEO_VERTICAL;
} else {
@ -1131,15 +1092,19 @@ public class LivePlayRyViewHolder extends LiveRoomPlayViewHolder {
}
@Override
public void onTimelineChanged(Timeline timeline, int reason) {
Player.Listener.super.onTimelineChanged(timeline, reason);
Log.i(TAG, "onTimelineChanged: " + reason);
public void onPlaybackStateChanged(int playbackState) {
Player.Listener.super.onPlaybackStateChanged(playbackState);
if (playbackState == Player.STATE_BUFFERING) {
showLoadingDialog();
}
}
@Override
public void onTracksChanged(Tracks tracks) {
Player.Listener.super.onTracksChanged(tracks);
Log.i(TAG, "onTracksChanged: " + tracks.getGroups().size());
public void onIsPlayingChanged(boolean isPlaying) {
Player.Listener.super.onIsPlayingChanged(isPlaying);
if (isPlaying) {
hideCover();
}
}
}
}

View File

@ -13,13 +13,14 @@ import com.yunbao.live.interfaces.ILiveLinkMicViewHolder;
public abstract class LiveRoomPlayViewHolder extends AbsViewHolder implements ILiveLinkMicViewHolder {
protected LiveBean mLiveBean;
public static final int PLAY_MODEL_DEF=-1;
public static final int PLAY_MODEL_480=0;
public static final int PLAY_MODEL_720=1;
public static final int PLAY_MODEL_1080=2;
public static final int PLAY_MODEL_DEF = -1;
public static final int PLAY_MODEL_480 = 0;
public static final int PLAY_MODEL_720 = 1;
public static final int PLAY_MODEL_1080 = 2;
public static int PLAY_MODEL = PLAY_MODEL_DEF;
public static final String[] videoRatioHorizontal = new String[]{"_640_480", "_960_720", "_1920_1080","_180_180"};
public static final String[] videoRatioVertical = new String[]{"_480_640", "_720_960", "_1080_1920","_180_180"};
public static final String[] videoRatioHorizontal = new String[]{"_640_480", "_1280_720", "_1920_1080", "_180_180"};
public static final String[] videoRatioVertical = new String[]{"_480_640", "_1280_960", "_1080_1920", "_180_180"};
public static final String[] videoFps = new String[]{"_24", "_30"};
OnMicCallback onMicCallback;//连麦回调
@ -27,7 +28,7 @@ public abstract class LiveRoomPlayViewHolder extends AbsViewHolder implements IL
super(context, parentView);
}
public abstract void play(String url,int playModel);
public abstract void play(String url, int playModel);
public abstract void stopPlay();
@ -66,7 +67,7 @@ public abstract class LiveRoomPlayViewHolder extends AbsViewHolder implements IL
}
public interface OnMicCallback{
public interface OnMicCallback {
void onMikUpdate();
}