优化播放抛错时重新播放

优化滑动直播间时可能出现上一个直播间画面的问题
This commit is contained in:
zlzw 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;
@ -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,7 +283,7 @@ public class LiveExoPlayerManager {
* @param url 地址
*/
public void switchUrl(String url) {
if(url.equals(this.url))return;
if (url!=null&&url.equals(this.url)) return;
Log.i(TAG, "switchUrl: " + url + " src : " + this.url);
this.url = url;
playBufferIndex = 0;
@ -316,7 +334,10 @@ public class LiveExoPlayerManager {
}
public void clearFrame() {
//mainView.setKeepContentOnPlayerReset(false);
mainView.setKeepContentOnPlayerReset(false);
if (mainView.getVideoSurfaceView() != null) {
mainView.setPlayer(null);
}
}
/**

View File

@ -370,6 +370,12 @@ public class LivePlayRyViewHolder extends LiveRoomPlayViewHolder {
}
}
@Override
public void clearFrame() {
super.clearFrame();
mPlayer.clearFrame();
}
@Override
public void release() {
mEnd = true;

View File

@ -97,6 +97,9 @@ public abstract class LiveRoomPlayViewHolder extends AbsViewHolder implements IL
public void switchStreamPk(boolean isPk) {
}
public void clearFrame() {
}
public interface OnMicCallback {
void onMikUpdate();

View File

@ -244,12 +244,19 @@ public class PortraitLiveManager implements LivePlayListener, SocketMessageListe
mViewPager.setCanScroll(true);
}
};
final Runnable loadRunnableGone = new Runnable() {
@Override
public void run() {
loading.setVisibility(View.GONE);
}
};
@Override
public synchronized void onAdd(LiveBean data, int liveType, int liveTypeVal, int liveSdk) {
isEnterRoom = false;
openParametersModel = new OpenParametersModel();
mContext.runOnUiThread(new Runnable() {
@Override
public void run() {
Log.e("ImgLoader1", data.getAvatar());
@ -264,9 +271,11 @@ public class PortraitLiveManager implements LivePlayListener, SocketMessageListe
.setmLiveType(mLiveType)
.setmLiveTypeVal(mLiveTypeVal)));
loading.setVisibility(View.VISIBLE);
loading.setAlpha(0.6f);
ImgLoader.displayBlurLive(mContext, mLiveBean.getAvatar(), ivBg);
AnimationDrawable frameAnimation = (AnimationDrawable) ivLoading.getBackground();
frameAnimation.start();
liveHandler.removeCallbacks(loadRunnableGone);
if (mLivePlayViewHolder == null) {
mLivePlayViewHolder = new LivePlayRyViewHolder(mContext, playContainer, 1);
mLiveRoomViewHolder = new LiveRoomViewHolder(false, 1, mContext, mContainer, mSecondPage.findViewById(R.id.gift_gif), mSecondPage.findViewById(R.id.gift_svga), mContainerWrap, mContext.getWindowManager());
@ -279,7 +288,7 @@ public class PortraitLiveManager implements LivePlayListener, SocketMessageListe
mLivePlayViewHolder.setLoadViewListener(new LiveRoomPlayViewHolder.LoadingListener() {
@Override
public void onPlayer() {
liveHandler.postDelayed(()->loading.setVisibility(View.GONE),200);
liveHandler.postDelayed(loadRunnableGone, 350);
}
});
mLiveRoomViewHolder.subscribeActivityLifeCycle();
@ -367,6 +376,7 @@ public class PortraitLiveManager implements LivePlayListener, SocketMessageListe
landscape = data.getLiveInfo().getLandscape();
mLivePlayViewHolder.setLiveBeanLandscape(landscape);
mLivePlayViewHolder.setLiveEnterRoomNewModel(data);
mLivePlayViewHolder.clearFrame();
int userClarityType = IMLoginManager.get(mContext).getInt(LiveRoomPlayViewHolder.PLAY_MODEL_KEY, -1);
int anchorClarityType = data.getClarityType() - 1;
if (userClarityType != anchorClarityType) {
@ -596,7 +606,7 @@ public class PortraitLiveManager implements LivePlayListener, SocketMessageListe
mLiveRoomViewHolder.showPrizePoolLevel(String.valueOf(giftPrizePoolLevel));
}
}
liveHandler.postDelayed(() -> loading.setVisibility(View.GONE), 10_000);
liveHandler.postDelayed(loadRunnableGone, 10_000);
}
@ -1794,6 +1804,15 @@ public class PortraitLiveManager implements LivePlayListener, SocketMessageListe
}
}
/**
* 幸运100%活动
*/
public void lucky100(MsgModel model){
if (mLiveRoomViewHolder != null) {
mLiveRoomViewHolder.lucky100(model);
}
}
public void messageBottom() {
if (mLiveRoomViewHolder != null) {
mLiveRoomViewHolder.chatScrollToBottom();