From 5b2b21da86684d78d22c2eaf4be689b06691c6c7 Mon Sep 17 00:00:00 2001 From: andrewlewis Date: Tue, 20 Aug 2019 08:40:44 +0100 Subject: [PATCH] Fix handling of delayed AdsLoader.start AdsMediaSource posts AdsLoader.start to the main thread during preparation, but the app may call AdsLoader.setPlayer(null) before it actually runs (e.g., if initializing then quickly backgrounding the player). This is valid usage of the API so handle this case instead of asserting. Because not calling setPlayer at all is a pitfall of the API, track whether setPlayer has been called and still assert that in AdsLoader.start. PiperOrigin-RevId: 264329632 --- .../android/exoplayer2/ext/ima/ImaAdsLoader.java | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/extensions/ima/src/main/java/com/google/android/exoplayer2/ext/ima/ImaAdsLoader.java b/extensions/ima/src/main/java/com/google/android/exoplayer2/ext/ima/ImaAdsLoader.java index 68e48b8d33..11071a7e4b 100644 --- a/extensions/ima/src/main/java/com/google/android/exoplayer2/ext/ima/ImaAdsLoader.java +++ b/extensions/ima/src/main/java/com/google/android/exoplayer2/ext/ima/ImaAdsLoader.java @@ -327,6 +327,7 @@ public final class ImaAdsLoader private final AdDisplayContainer adDisplayContainer; private final com.google.ads.interactivemedia.v3.api.AdsLoader adsLoader; + private boolean wasSetPlayerCalled; @Nullable private Player nextPlayer; private Object pendingAdRequestContext; private List supportedMimeTypes; @@ -558,6 +559,7 @@ public final class ImaAdsLoader Assertions.checkState( player == null || player.getApplicationLooper() == Looper.getMainLooper()); nextPlayer = player; + wasSetPlayerCalled = true; } @Override @@ -585,9 +587,12 @@ public final class ImaAdsLoader @Override public void start(EventListener eventListener, AdViewProvider adViewProvider) { - Assertions.checkNotNull( - nextPlayer, "Set player using adsLoader.setPlayer before preparing the player."); + Assertions.checkState( + wasSetPlayerCalled, "Set player using adsLoader.setPlayer before preparing the player."); player = nextPlayer; + if (player == null) { + return; + } this.eventListener = eventListener; lastVolumePercentage = 0; lastAdProgress = null; @@ -617,6 +622,9 @@ public final class ImaAdsLoader @Override public void stop() { + if (player == null) { + return; + } if (adsManager != null && imaPausedContent) { adPlaybackState = adPlaybackState.withAdResumePositionUs(