From 4428adf4c1d483e7d36e20e5800b434f32811cd2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20Velad=20Galv=C3=A1n?= Date: Mon, 28 Aug 2023 19:05:42 +0200 Subject: [PATCH] fix(Ads): Initialize correctly the IMA ads manager (#5541) Fixes https://github.com/shaka-project/shaka-player/issues/4800 --- lib/ads/client_side_ad_manager.js | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/ads/client_side_ad_manager.js b/lib/ads/client_side_ad_manager.js index 321f644588..92bec91766 100644 --- a/lib/ads/client_side_ad_manager.js +++ b/lib/ads/client_side_ad_manager.js @@ -34,6 +34,9 @@ shaka.ads.ClientSideAdManager = class { /** @private {HTMLMediaElement} */ this.video_ = video; + /** @private {boolean} */ + this.videoPlayed_ = false; + /** @private {?shaka.extern.AdsConfiguration} */ this.config_ = null; @@ -92,6 +95,10 @@ shaka.ads.ClientSideAdManager = class { this.eventManager_.listen(this.video_, 'ended', () => { this.adsLoader_.contentComplete(); }); + + this.eventManager_.listenOnce(this.video_, 'play', () => { + this.videoPlayed_ = true; + }); } /** @@ -255,9 +262,14 @@ shaka.ads.ClientSideAdManager = class { // seen the ads actually play until requestAds() is called. // Note: We listen for a play event to avoid autoplay issues that might // crash IMA. - this.eventManager_.listenOnce(this.video_, 'play', () => { + if (this.videoPlayed_) { this.imaAdsManager_.start(); - }); + } else { + this.eventManager_.listenOnce(this.video_, 'play', () => { + this.videoPlayed_ = true; + this.imaAdsManager_.start(); + }); + } } catch (adError) { // If there was a problem with the VAST response, // we we won't be getting an ad. Hide ad UI if we showed it already