mirror of
https://github.com/androidx/media.git
synced 2025-05-05 06:30:24 +08:00
Defer adsManager.init until the timeline has loaded
If the app seeks after we get an ads manager but before the player exposes the timeline with ads, we would end up expecting to play a preroll even after the seek request arrived. This caused the player to get stuck. Wait until a non-empty timeline has been exposed via onTimelineChanged before initializing IMA (at which point it can start polling the player position). Seek requests are not handled while an ad is playing. PiperOrigin-RevId: 265058325
This commit is contained in:
parent
7cefb56eda
commit
0085a7e761
@ -338,6 +338,7 @@ public final class ImaAdsLoader
|
|||||||
private int lastVolumePercentage;
|
private int lastVolumePercentage;
|
||||||
|
|
||||||
private AdsManager adsManager;
|
private AdsManager adsManager;
|
||||||
|
private boolean initializedAdsManager;
|
||||||
private AdLoadException pendingAdLoadError;
|
private AdLoadException pendingAdLoadError;
|
||||||
private Timeline timeline;
|
private Timeline timeline;
|
||||||
private long contentDurationMs;
|
private long contentDurationMs;
|
||||||
@ -613,8 +614,8 @@ public final class ImaAdsLoader
|
|||||||
adsManager.resume();
|
adsManager.resume();
|
||||||
}
|
}
|
||||||
} else if (adsManager != null) {
|
} else if (adsManager != null) {
|
||||||
// Ads have loaded but the ads manager is not initialized.
|
adPlaybackState = new AdPlaybackState(getAdGroupTimesUs(adsManager.getAdCuePoints()));
|
||||||
startAdPlayback();
|
updateAdPlaybackState();
|
||||||
} else {
|
} else {
|
||||||
// Ads haven't loaded yet, so request them.
|
// Ads haven't loaded yet, so request them.
|
||||||
requestAds(adViewGroup);
|
requestAds(adViewGroup);
|
||||||
@ -688,7 +689,8 @@ public final class ImaAdsLoader
|
|||||||
if (player != null) {
|
if (player != null) {
|
||||||
// If a player is attached already, start playback immediately.
|
// If a player is attached already, start playback immediately.
|
||||||
try {
|
try {
|
||||||
startAdPlayback();
|
adPlaybackState = new AdPlaybackState(getAdGroupTimesUs(adsManager.getAdCuePoints()));
|
||||||
|
updateAdPlaybackState();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
maybeNotifyInternalError("onAdsManagerLoaded", e);
|
maybeNotifyInternalError("onAdsManagerLoaded", e);
|
||||||
}
|
}
|
||||||
@ -968,6 +970,10 @@ public final class ImaAdsLoader
|
|||||||
if (contentDurationUs != C.TIME_UNSET) {
|
if (contentDurationUs != C.TIME_UNSET) {
|
||||||
adPlaybackState = adPlaybackState.withContentDurationUs(contentDurationUs);
|
adPlaybackState = adPlaybackState.withContentDurationUs(contentDurationUs);
|
||||||
}
|
}
|
||||||
|
if (!initializedAdsManager && adsManager != null) {
|
||||||
|
initializedAdsManager = true;
|
||||||
|
initializeAdsManager();
|
||||||
|
}
|
||||||
onPositionDiscontinuity(Player.DISCONTINUITY_REASON_INTERNAL);
|
onPositionDiscontinuity(Player.DISCONTINUITY_REASON_INTERNAL);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1041,7 +1047,7 @@ public final class ImaAdsLoader
|
|||||||
|
|
||||||
// Internal methods.
|
// Internal methods.
|
||||||
|
|
||||||
private void startAdPlayback() {
|
private void initializeAdsManager() {
|
||||||
AdsRenderingSettings adsRenderingSettings = imaFactory.createAdsRenderingSettings();
|
AdsRenderingSettings adsRenderingSettings = imaFactory.createAdsRenderingSettings();
|
||||||
adsRenderingSettings.setEnablePreloading(ENABLE_PRELOADING);
|
adsRenderingSettings.setEnablePreloading(ENABLE_PRELOADING);
|
||||||
adsRenderingSettings.setMimeTypes(supportedMimeTypes);
|
adsRenderingSettings.setMimeTypes(supportedMimeTypes);
|
||||||
@ -1056,10 +1062,9 @@ public final class ImaAdsLoader
|
|||||||
adsRenderingSettings.setUiElements(adUiElements);
|
adsRenderingSettings.setUiElements(adUiElements);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set up the ad playback state, skipping ads based on the start position as required.
|
// Skip ads based on the start position as required.
|
||||||
long[] adGroupTimesUs = getAdGroupTimesUs(adsManager.getAdCuePoints());
|
long[] adGroupTimesUs = getAdGroupTimesUs(adsManager.getAdCuePoints());
|
||||||
adPlaybackState = new AdPlaybackState(adGroupTimesUs);
|
long contentPositionMs = player.getContentPosition();
|
||||||
long contentPositionMs = player.getCurrentPosition();
|
|
||||||
int adGroupIndexForPosition =
|
int adGroupIndexForPosition =
|
||||||
adPlaybackState.getAdGroupIndexForPositionUs(C.msToUs(contentPositionMs));
|
adPlaybackState.getAdGroupIndexForPositionUs(C.msToUs(contentPositionMs));
|
||||||
if (adGroupIndexForPosition > 0 && adGroupIndexForPosition != C.INDEX_UNSET) {
|
if (adGroupIndexForPosition > 0 && adGroupIndexForPosition != C.INDEX_UNSET) {
|
||||||
@ -1093,7 +1098,6 @@ public final class ImaAdsLoader
|
|||||||
pendingContentPositionMs = contentPositionMs;
|
pendingContentPositionMs = contentPositionMs;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start ad playback.
|
|
||||||
adsManager.init(adsRenderingSettings);
|
adsManager.init(adsRenderingSettings);
|
||||||
updateAdPlaybackState();
|
updateAdPlaybackState();
|
||||||
if (DEBUG) {
|
if (DEBUG) {
|
||||||
|
@ -143,7 +143,8 @@ public class ImaAdsLoaderTest {
|
|||||||
assertThat(adsLoaderListener.adPlaybackState)
|
assertThat(adsLoaderListener.adPlaybackState)
|
||||||
.isEqualTo(
|
.isEqualTo(
|
||||||
new AdPlaybackState(/* adGroupTimesUs= */ 0)
|
new AdPlaybackState(/* adGroupTimesUs= */ 0)
|
||||||
.withAdDurationsUs(PREROLL_ADS_DURATIONS_US));
|
.withAdDurationsUs(PREROLL_ADS_DURATIONS_US)
|
||||||
|
.withContentDurationUs(CONTENT_DURATION_US));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
Loading…
x
Reference in New Issue
Block a user