diff --git a/src/plugin.js b/src/plugin.js index 2349fb32..e86f330c 100644 --- a/src/plugin.js +++ b/src/plugin.js @@ -139,6 +139,7 @@ const contribAdsPlugin = function(options) { // Restart the cancelContentPlay process. player.on('playing', () => { player.ads._cancelledPlay = false; + player.ads._pausedOnContentupdate = false; }); player.one('loadstart', () => { @@ -638,10 +639,10 @@ const contribAdsPlugin = function(options) { triggerevent: player.ads.triggerevent }); - // Play the content if cancelContentPlay happened and we haven't played yet. - // This happens if there was no preroll or if it errored, timed out, etc. - // Otherwise snapshot restore would play. - if (player.ads._cancelledPlay) { + // Play the content if cancelContentPlay happened or we paused on 'contentupdate' + // and we haven't played yet. This happens if there was no preroll or if it + // errored, timed out, etc. Otherwise snapshot restore would play. + if (player.ads._cancelledPlay || player.ads._pausedOnContentupdate) { if (player.paused()) { player.play(); } @@ -666,6 +667,10 @@ const contribAdsPlugin = function(options) { if (player.paused()) { this.state = 'content-set'; } else { + // If we get here we missed the 'play' event, so pause the player so + // that content doesn't start playing immediately after the source change + player.pause(); + player.ads._pausedOnContentupdate = true; this.state = 'ads-ready?'; } }, diff --git a/test/test.ads.js b/test/test.ads.js index ab081024..a4432b94 100644 --- a/test/test.ads.js +++ b/test/test.ads.js @@ -729,6 +729,18 @@ QUnit.test('adsready in content-playback triggers readyforpreroll', function(ass assert.strictEqual(spy.getCall(0).args[0].type, 'readyforpreroll', 'readyforpreroll should have been triggered.'); }); +QUnit.test('contentupdate in content-playback transitions to ads-ready? and pauses player if not already paused', function(assert) { + this.player.trigger('loadstart'); + this.player.ads.skipLinearAdMode(); + this.player.paused = function() { + return false; + }; + sinon.spy(this.player, 'pause'); + this.player.trigger('contentupdate'); + assert.ok(this.player.pause.calledOnce, 'player was paused'); + assert.ok(this.player.ads._pausedOnContentupdate, '_pausedOnContentupdate is true'); +}); + // ---------------------------------- // Event prefixing during ad playback // ----------------------------------