update 清晰度切换

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

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