Skip to content

Commit

Permalink
fix(Ads): Initialize correctly the IMA ads manager (#5541)
Browse files Browse the repository at this point in the history
Fixes #4800

Backported to v4.3.x
  • Loading branch information
avelad authored and joeyparrish committed Aug 30, 2023
1 parent 31f7a18 commit fa82928
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions lib/ads/client_side_ad_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ shaka.ads.ClientSideAdManager = class {
/** @private {HTMLMediaElement} */
this.video_ = video;

/** @private {boolean} */
this.videoPlayed_ = false;

/** @private {ResizeObserver} */
this.resizeObserver_ = null;

Expand Down Expand Up @@ -82,6 +85,10 @@ shaka.ads.ClientSideAdManager = class {
this.eventManager_.listen(this.video_, 'ended', () => {
this.adsLoader_.contentComplete();
});

this.eventManager_.listenOnce(this.video_, 'play', () => {
this.videoPlayed_ = true;
});
}

/**
Expand Down Expand Up @@ -219,9 +226,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
Expand Down

0 comments on commit fa82928

Please sign in to comment.