diff --git a/playkit/src/main/java/com/kaltura/playkit/player/PlayerController.java b/playkit/src/main/java/com/kaltura/playkit/player/PlayerController.java index a0f800c39..dee644955 100644 --- a/playkit/src/main/java/com/kaltura/playkit/player/PlayerController.java +++ b/playkit/src/main/java/com/kaltura/playkit/player/PlayerController.java @@ -182,6 +182,11 @@ public Player.Settings getSettings() { public void prepare(@NonNull PKMediaConfig mediaConfig) { isNewEntry = isNewEntry(mediaConfig); + if (mediaConfig == null) { + log.e("No playable mediaConfig found, mediaConfig = null"); + return; + } + this.mediaConfig = mediaConfig; PKMediaSource source = SourceSelector.selectSource(mediaConfig.getMediaEntry()); diff --git a/playkit/src/main/java/com/kaltura/playkit/plugins/ads/ima/IMAPlugin.java b/playkit/src/main/java/com/kaltura/playkit/plugins/ads/ima/IMAPlugin.java index b36e2d1de..aec1afeb4 100644 --- a/playkit/src/main/java/com/kaltura/playkit/plugins/ads/ima/IMAPlugin.java +++ b/playkit/src/main/java/com/kaltura/playkit/plugins/ads/ima/IMAPlugin.java @@ -8,6 +8,7 @@ import com.google.ads.interactivemedia.v3.api.Ad; import com.google.ads.interactivemedia.v3.api.AdDisplayContainer; import com.google.ads.interactivemedia.v3.api.AdErrorEvent; +import com.google.ads.interactivemedia.v3.api.AdPodInfo; import com.google.ads.interactivemedia.v3.api.AdsLoader; import com.google.ads.interactivemedia.v3.api.AdsManager; import com.google.ads.interactivemedia.v3.api.AdsManagerLoadedEvent; @@ -147,7 +148,6 @@ protected void onLoad(Player player, Object config, final MessageBus messageBus, log.e("Error, player instance is null."); return; } - this.mediaConfig = mediaConfig; this.context = context; this.messageBus = messageBus; this.messageBus.listen(new PKEvent.Listener() { @@ -203,10 +203,7 @@ public void onFinish() { log.d("adManagerTimer.onFinish, adsManager=" + adsManager); if (adsManager == null) { log.d("adsManager is null, will play content"); - if (pkAdProviderListener != null) { - isContentPrepared = true; - pkAdProviderListener.onAdLoadingFinished(); - } + preparePlayerOnAdLoadingFinished(); messageBus.post(new AdEvent(AdEvent.Type.AD_BREAK_IGNORED)); if (isAdRequested) { @@ -215,8 +212,10 @@ public void onFinish() { } } }; - adManagerTimer.start(); - messageBus.post(new AdEvent(AD_LOAD_TIMEOUT_TIMER_STARTED)); + if (adManagerTimer != null) { + adManagerTimer.start(); + messageBus.post(new AdEvent(AD_LOAD_TIMEOUT_TIMER_STARTED)); + } } adDisplayContainer = sdkFactory.createAdDisplayContainer(); @@ -334,10 +333,7 @@ protected void onDestroy() { cancelAdDisplayedCheckTimer(); isContentPrepared = false; - if (adManagerTimer != null) { - adManagerTimer.cancel(); - adManagerTimer = null; - } + cancelAdManagerTimer(); adTagCuePoints = null; adPlaybackCancelled = false; @@ -356,6 +352,14 @@ protected void onDestroy() { removeAdProviderListener(); } + private void cancelAdManagerTimer() { + if (adManagerTimer != null) { + log.d("cancelAdManagerTimer"); + adManagerTimer.cancel(); + adManagerTimer = null; + } + } + ////////Ads Plugin @Override @@ -372,8 +376,7 @@ private AdsLoader.AdsLoadedListener getAdsLoadedListener() { public void onAdsManagerLoaded(AdsManagerLoadedEvent adsManagerLoadedEvent) { log.d("AdsManager loaded"); - - adManagerTimer.cancel(); + cancelAdManagerTimer(); // Ads were successfully loaded, so get the AdsManager instance. AdsManager has // events for ad playback and errors. @@ -620,20 +623,20 @@ public void onAdEvent(com.google.ads.interactivemedia.v3.api.AdEvent adEvent) { adsManager.pause(); } - adInfo = createAdInfo(adEvent.getAd()); messageBus.post(new AdEvent.AdStartedEvent(adInfo)); + if (!appIsInBackground) { + preparePlayerOnAdLoadingFinished(); + } + if (adTagCuePoints == null) { Handler handler = new Handler(); handler.postDelayed(new Runnable() { @Override public void run() { log.d("AD CUEPOINTS CHANGED TRIGGERED WITH DELAY"); - if (pkAdProviderListener != null) { - isContentPrepared = true; - pkAdProviderListener.onAdLoadingFinished(); - } + adTagCuePoints = new AdCuePoints(getAdCuePoints()); messageBus.post(new AdEvent.AdCuePointsUpdateEvent(adTagCuePoints)); } @@ -705,13 +708,23 @@ public void run() { break; case LOG: //for this case no AD ERROR is fired need to show view {type=adLoadError, errorCode=1009, errorMessage=The response does not contain any valid ads.} - if (pkAdProviderListener != null) { - isContentPrepared = true; - pkAdProviderListener.onAdLoadingFinished(); - } + preparePlayerOnAdLoadingFinished(); if (!isAdDisplayed) { cancelAdDisplayedCheckTimer(); } + Ad adInfo = adEvent.getAd(); + if (adInfo != null) { + //incase one ad in the pod fails to play we want next one to be played + AdPodInfo adPoidInfo = adInfo.getAdPodInfo(); + log.d("adPoidInfo.getAdPosition() = " + adPoidInfo.getAdPosition() + " adPoidInfo.getTotalAds() = " + adPoidInfo.getTotalAds()); + if (adPoidInfo != null && adPoidInfo.getTotalAds() > 1 && adPoidInfo.getAdPosition() < adPoidInfo.getTotalAds()) { + return; + } else { + adsManager.discardAdBreak(); + } + } else { + adsManager.discardAdBreak(); + } log.e("Ad LogError - back to playback"); if (player != null && player.getView() != null) { @@ -827,6 +840,7 @@ public void onAdError(AdErrorEvent adErrorEvent) { isAdDisplayed = false; cancelAdDisplayedCheckTimer(); + cancelAdManagerTimer(); String errorMessage = adErrorEvent.getError().getMessage(); switch (adErrorEvent.getError().getErrorCode()) { case INTERNAL_ERROR: @@ -890,14 +904,18 @@ public void onAdError(AdErrorEvent adErrorEvent) { messageBus.post(new AdError(AdError.Type.UNKNOWN_ERROR, errorMessage)); } - if (pkAdProviderListener != null) { - isContentPrepared = true; - pkAdProviderListener.onAdLoadingFinished(); - } + preparePlayerOnAdLoadingFinished(); if (player != null && player.getView() != null) { player.getView().showVideoSurface(); player.play(); } } + + private void preparePlayerOnAdLoadingFinished() { + if (pkAdProviderListener != null) { + isContentPrepared = true; + pkAdProviderListener.onAdLoadingFinished(); + } + } }