优化播放抛错时重新播放

优化滑动直播间时可能出现上一个直播间画面的问题
This commit is contained in:
2023-01-10 16:12:17 +08:00
parent 518e57ee44
commit 3606815a41
4 changed files with 59 additions and 10 deletions

View File

@@ -11,6 +11,7 @@ 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.PlaybackException;
import com.google.android.exoplayer2.Player;
import com.google.android.exoplayer2.analytics.AnalyticsListener;
import com.google.android.exoplayer2.ui.AspectRatioFrameLayout;
@@ -34,7 +35,7 @@ public class LiveExoPlayerManager {
private int playBufferIndex = 0;//卡顿计数器
private final Handler handler;
private static double log_buffer_time = 0, log_buffer_max_time;
private String url="";
private String url = "";
public LiveExoPlayerManager(Context mContext) {
DefaultLoadControl control = new DefaultLoadControl.Builder()
@@ -123,7 +124,7 @@ public class LiveExoPlayerManager {
public void onPlaybackStateChanged(int playbackState) {
Player.Listener.super.onPlaybackStateChanged(playbackState);
Log.i(TAG, "onPlaybackStateChanged 1: " + playbackState);
if (playbackState == Player.STATE_READY) {
if (playbackState == Player.STATE_READY && !player1.isPlaying()) {
player2.stop();
player2.clearVideoSurface();
player1.play();
@@ -177,6 +178,15 @@ public class LiveExoPlayerManager {
Player.Listener.super.onIsLoadingChanged(isLoading);
Log.i(TAG, "onIsLoadingChanged: 1 " + isLoading);
}
@Override
public void onPlayerError(@NonNull PlaybackException error) {
Player.Listener.super.onPlayerError(error);
error.printStackTrace();
String url=LiveExoPlayerManager.this.url;
LiveExoPlayerManager.this.url=null;
startUrl(url);
}
});
player2.addListener(new Player.Listener() {
@@ -184,7 +194,7 @@ public class LiveExoPlayerManager {
public void onPlaybackStateChanged(int playbackState) {
Player.Listener.super.onPlaybackStateChanged(playbackState);
Log.i(TAG, "onPlaybackStateChanged 2: " + playbackState);
if (playbackState == Player.STATE_READY) {
if (playbackState == Player.STATE_READY && !player2.isPlaying()) {
player1.stop();
player1.clearVideoSurface();
player2.play();
@@ -235,6 +245,14 @@ public class LiveExoPlayerManager {
Player.Listener.super.onIsLoadingChanged(isLoading);
Log.i(TAG, "onIsLoadingChanged: 2 " + isLoading);
}
@Override
public void onPlayerError(@NonNull PlaybackException error) {
Player.Listener.super.onPlayerError(error);
error.printStackTrace();
String url=LiveExoPlayerManager.this.url;
LiveExoPlayerManager.this.url=null;
startUrl(url);
}
});
}
@@ -244,7 +262,7 @@ public class LiveExoPlayerManager {
* @param url 地址
*/
public void startUrl(String url) {
if(url.equals(this.url))return;
if (url!=null&&url.equals(this.url)) return;
Log.i(TAG, "startUrl: " + url + " > " + mainView.getResizeMode());
this.url = url;
isSwitchUrl = true;
@@ -265,8 +283,8 @@ public class LiveExoPlayerManager {
* @param url 地址
*/
public void switchUrl(String url) {
if(url.equals(this.url))return;
Log.i(TAG, "switchUrl: " + url +" src : "+this.url);
if (url!=null&&url.equals(this.url)) return;
Log.i(TAG, "switchUrl: " + url + " src : " + this.url);
this.url = url;
playBufferIndex = 0;
isSwitchUrl = true;
@@ -316,7 +334,10 @@ public class LiveExoPlayerManager {
}
public void clearFrame() {
//mainView.setKeepContentOnPlayerReset(false);
mainView.setKeepContentOnPlayerReset(false);
if (mainView.getVideoSurfaceView() != null) {
mainView.setPlayer(null);
}
}
/**