Clean up player event handling
This change is similar to e8293b92d877bd6e5a00606358db610d6fb7a4d6 but without relying on new player events that haven't been released yet, to make it easier to merge changes related to ImaAdsLoader on top.
This commit is contained in:
parent
b3de2b08d4
commit
03be1551a7
@ -332,7 +332,7 @@ public final class ImaAdsLoader
|
||||
private VideoProgressUpdate lastAdProgress;
|
||||
private int lastVolumePercentage;
|
||||
|
||||
private AdsManager adsManager;
|
||||
@Nullable private AdsManager adsManager;
|
||||
private boolean initializedAdsManager;
|
||||
private AdLoadException pendingAdLoadError;
|
||||
private Timeline timeline;
|
||||
@ -975,12 +975,12 @@ public final class ImaAdsLoader
|
||||
initializedAdsManager = true;
|
||||
initializeAdsManager();
|
||||
}
|
||||
onPositionDiscontinuity(Player.DISCONTINUITY_REASON_INTERNAL);
|
||||
checkForContentCompleteOrNewAdGroup();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPlayerStateChanged(boolean playWhenReady, @Player.State int playbackState) {
|
||||
if (adsManager == null) {
|
||||
if (adsManager == null || player == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
@ -993,18 +993,7 @@ public final class ImaAdsLoader
|
||||
adsManager.resume();
|
||||
return;
|
||||
}
|
||||
|
||||
if (imaAdState == IMA_AD_STATE_NONE && playbackState == Player.STATE_BUFFERING
|
||||
&& playWhenReady) {
|
||||
checkForContentComplete();
|
||||
} else if (imaAdState != IMA_AD_STATE_NONE && playbackState == Player.STATE_ENDED) {
|
||||
for (int i = 0; i < adCallbacks.size(); i++) {
|
||||
adCallbacks.get(i).onEnded();
|
||||
}
|
||||
if (DEBUG) {
|
||||
Log.d(TAG, "VideoAdPlayerCallback.onEnded in onPlayerStateChanged");
|
||||
}
|
||||
}
|
||||
handlePlayerStateChanged(playWhenReady, playbackState);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -1018,32 +1007,7 @@ public final class ImaAdsLoader
|
||||
|
||||
@Override
|
||||
public void onPositionDiscontinuity(@Player.DiscontinuityReason int reason) {
|
||||
if (adsManager == null) {
|
||||
return;
|
||||
}
|
||||
if (!playingAd && !player.isPlayingAd()) {
|
||||
checkForContentComplete();
|
||||
if (sentContentComplete) {
|
||||
for (int i = 0; i < adPlaybackState.adGroupCount; i++) {
|
||||
if (adPlaybackState.adGroupTimesUs[i] != C.TIME_END_OF_SOURCE) {
|
||||
adPlaybackState = adPlaybackState.withSkippedAdGroup(i);
|
||||
}
|
||||
}
|
||||
updateAdPlaybackState();
|
||||
} else if (!timeline.isEmpty()) {
|
||||
long positionMs = player.getCurrentPosition();
|
||||
timeline.getPeriod(0, period);
|
||||
int newAdGroupIndex = period.getAdGroupIndexForPositionUs(C.msToUs(positionMs));
|
||||
if (newAdGroupIndex != C.INDEX_UNSET) {
|
||||
sentPendingContentPositionMs = false;
|
||||
pendingContentPositionMs = positionMs;
|
||||
if (newAdGroupIndex != adGroupIndex) {
|
||||
shouldNotifyAdPrepareError = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
updateImaStateForPlayerState();
|
||||
checkForContentCompleteOrNewAdGroup();
|
||||
}
|
||||
|
||||
// Internal methods.
|
||||
@ -1176,6 +1140,50 @@ public final class ImaAdsLoader
|
||||
}
|
||||
}
|
||||
|
||||
private void handlePlayerStateChanged(boolean playWhenReady, @Player.State int playbackState) {
|
||||
if (imaAdState == IMA_AD_STATE_NONE
|
||||
&& playbackState == Player.STATE_BUFFERING
|
||||
&& playWhenReady) {
|
||||
checkForContentComplete();
|
||||
} else if (imaAdState != IMA_AD_STATE_NONE && playbackState == Player.STATE_ENDED) {
|
||||
for (int i = 0; i < adCallbacks.size(); i++) {
|
||||
adCallbacks.get(i).onEnded();
|
||||
}
|
||||
if (DEBUG) {
|
||||
Log.d(TAG, "VideoAdPlayerCallback.onEnded in onPlayerStateChanged");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void checkForContentCompleteOrNewAdGroup() {
|
||||
if (adsManager == null || player == null) {
|
||||
return;
|
||||
}
|
||||
if (!playingAd && !player.isPlayingAd()) {
|
||||
checkForContentComplete();
|
||||
if (sentContentComplete) {
|
||||
for (int i = 0; i < adPlaybackState.adGroupCount; i++) {
|
||||
if (adPlaybackState.adGroupTimesUs[i] != C.TIME_END_OF_SOURCE) {
|
||||
adPlaybackState = adPlaybackState.withSkippedAdGroup(/* adGroupIndex= */ i);
|
||||
}
|
||||
}
|
||||
updateAdPlaybackState();
|
||||
} else if (!timeline.isEmpty()) {
|
||||
long positionMs = player.getCurrentPosition();
|
||||
timeline.getPeriod(/* periodIndex= */ 0, period);
|
||||
int newAdGroupIndex = period.getAdGroupIndexForPositionUs(C.msToUs(positionMs));
|
||||
if (newAdGroupIndex != C.INDEX_UNSET) {
|
||||
sentPendingContentPositionMs = false;
|
||||
pendingContentPositionMs = positionMs;
|
||||
if (newAdGroupIndex != adGroupIndex) {
|
||||
shouldNotifyAdPrepareError = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
updateImaStateForPlayerState();
|
||||
}
|
||||
|
||||
private void updateImaStateForPlayerState() {
|
||||
boolean wasPlayingAd = playingAd;
|
||||
int oldPlayingAdIndexInAdGroup = playingAdIndexInAdGroup;
|
||||
|
Loading…
x
Reference in New Issue
Block a user