diff options
author | Sam Potts <sam@potts.es> | 2019-03-16 12:14:20 +1100 |
---|---|---|
committer | Sam Potts <sam@potts.es> | 2019-03-16 12:14:20 +1100 |
commit | 35f7ee9c59ff082a5b71aae43ffccab4cdf10fdf (patch) | |
tree | 75b8f7c56ec7fa6696991e52197172c9c6c7c3cd /src/js/plugins/ads.js | |
parent | bdd513635fffa33f66735c80209e6ae77e0426b4 (diff) | |
parent | c202551e6d0b11656a99b41f3f8b3a48f2bf1e0a (diff) | |
download | plyr-35f7ee9c59ff082a5b71aae43ffccab4cdf10fdf.tar.lz plyr-35f7ee9c59ff082a5b71aae43ffccab4cdf10fdf.tar.xz plyr-35f7ee9c59ff082a5b71aae43ffccab4cdf10fdf.zip |
Merge branch 'develop' into css-variables
# Conflicts:
# demo/dist/demo.css
# demo/index.html
# dist/plyr.css
# gulpfile.js
# package.json
# yarn.lock
Diffstat (limited to 'src/js/plugins/ads.js')
-rw-r--r-- | src/js/plugins/ads.js | 101 |
1 files changed, 62 insertions, 39 deletions
diff --git a/src/js/plugins/ads.js b/src/js/plugins/ads.js index 375fdc13..c9256b0e 100644 --- a/src/js/plugins/ads.js +++ b/src/js/plugins/ads.js @@ -17,12 +17,12 @@ import { buildUrlParams } from '../utils/urls'; class Ads { /** * Ads constructor. - * @param {object} player + * @param {Object} player * @return {Ads} */ constructor(player) { this.player = player; - this.publisherId = player.config.ads.publisherId; + this.config = player.config.ads; this.playing = false; this.initialized = false; this.elements = { @@ -49,8 +49,13 @@ class Ads { } get enabled() { + const { config } = this; + return ( - this.player.isHTML5 && this.player.isVideo && this.player.config.ads.enabled && !is.empty(this.publisherId) + this.player.isHTML5 && + this.player.isVideo && + config.enabled && + (!is.empty(config.publisherId) || is.url(config.tagUrl)) ); } @@ -95,8 +100,14 @@ class Ads { this.setupIMA(); } - // Build the default tag URL + // Build the tag URL get tagUrl() { + const { config } = this; + + if (is.url(config.tagUrl)) { + return config.tagUrl; + } + const params = { AV_PUBLISHERID: '58c25bb0073ef448b1087ad6', AV_CHANNELID: '5a0458dc28a06145e4519d21', @@ -125,6 +136,7 @@ class Ads { this.elements.container = createElement('div', { class: this.player.config.classNames.ads, }); + this.player.elements.container.appendChild(this.elements.container); // So we can run VPAID2 @@ -133,9 +145,11 @@ class Ads { // Set language google.ima.settings.setLocale(this.player.config.ads.language); - // We assume the adContainer is the video container of the plyr element - // that will house the ads - this.elements.displayContainer = new google.ima.AdDisplayContainer(this.elements.container); + // Set playback for iOS10+ + google.ima.settings.setDisableCustomPlaybackForIOS10Plus(this.player.config.playsinline); + + // We assume the adContainer is the video container of the plyr element that will house the ads + this.elements.displayContainer = new google.ima.AdDisplayContainer(this.elements.container, this.player.media); // Request video ads to be pre-loaded this.requestAds(); @@ -184,7 +198,7 @@ class Ads { /** * Update the ad countdown - * @param {boolean} start + * @param {Boolean} start */ pollCountdown(start = false) { if (!start) { @@ -226,6 +240,23 @@ class Ads { // Get the cue points for any mid-rolls by filtering out the pre- and post-roll this.cuePoints = this.manager.getCuePoints(); + // Set volume to match player + this.manager.setVolume(this.player.volume); + + // Add listeners to the required events + // Advertisement error events + this.manager.addEventListener(google.ima.AdErrorEvent.Type.AD_ERROR, error => this.onAdError(error)); + + // Advertisement regular events + Object.keys(google.ima.AdEvent.Type).forEach(type => { + this.manager.addEventListener(google.ima.AdEvent.Type[type], event => this.onAdEvent(event)); + }); + + // Resolve our adsManager + this.trigger('loaded'); + } + + addCuePoints() { // Add advertisement cue's within the time line if available if (!is.empty(this.cuePoints)) { this.cuePoints.forEach(cuePoint => { @@ -233,7 +264,7 @@ class Ads { const seekElement = this.player.elements.progress; if (is.element(seekElement)) { - const cuePercentage = 100 / this.player.duration * cuePoint; + const cuePercentage = (100 / this.player.duration) * cuePoint; const cue = createElement('span', { class: this.player.config.classNames.cues, }); @@ -244,21 +275,6 @@ class Ads { } }); } - - // Set volume to match player - this.manager.setVolume(this.player.volume); - - // Add listeners to the required events - // Advertisement error events - this.manager.addEventListener(google.ima.AdErrorEvent.Type.AD_ERROR, error => this.onAdError(error)); - - // Advertisement regular events - Object.keys(google.ima.AdEvent.Type).forEach(type => { - this.manager.addEventListener(google.ima.AdEvent.Type[type], event => this.onAdEvent(event)); - }); - - // Resolve our adsManager - this.trigger('loaded'); } /** @@ -273,6 +289,7 @@ class Ads { // Retrieve the ad from the event. Some events (e.g. ALL_ADS_COMPLETED) // don't have ad object associated const ad = event.getAd(); + const adData = event.getAdData(); // Proxy event const dispatchEvent = type => { @@ -368,6 +385,12 @@ class Ads { dispatchEvent(event.type); break; + case google.ima.AdEvent.Type.LOG: + if (adData.adError) { + this.player.debug.warn(`Non-fatal ad error: ${adData.adError.getMessage()}`); + } + break; + default: break; } @@ -391,14 +414,16 @@ class Ads { const { container } = this.player.elements; let time; - // Add listeners to the required events + this.player.on('canplay', () => { + this.addCuePoints(); + }); + this.player.on('ended', () => { this.loader.contentComplete(); }); - this.player.on('seeking', () => { + this.player.on('timeupdate', () => { time = this.player.currentTime; - return time; }); this.player.on('seeked', () => { @@ -471,10 +496,8 @@ class Ads { // Ad is stopped this.playing = false; - // Play our video - if (this.player.currentTime < this.player.duration) { - this.player.play(); - } + // Play video + this.player.media.play(); } /** @@ -484,11 +507,11 @@ class Ads { // Show the advertisement container this.elements.container.style.zIndex = 3; - // Ad is playing. + // Ad is playing this.playing = true; // Pause our video. - this.player.pause(); + this.player.media.pause(); } /** @@ -536,7 +559,7 @@ class Ads { /** * Handles callbacks after an ad event was invoked - * @param {string} event - Event type + * @param {String} event - Event type */ trigger(event, ...args) { const handlers = this.events[event]; @@ -552,8 +575,8 @@ class Ads { /** * Add event listeners - * @param {string} event - Event type - * @param {function} callback - Callback for when event occurs + * @param {String} event - Event type + * @param {Function} callback - Callback for when event occurs * @return {Ads} */ on(event, callback) { @@ -571,8 +594,8 @@ class Ads { * The advertisement has 12 seconds to get its things together. We stop this timer when the * advertisement is playing, or when a user action is required to start, then we clear the * timer on ad ready - * @param {number} time - * @param {string} from + * @param {Number} time + * @param {String} from */ startSafetyTimer(time, from) { this.player.debug.log(`Safety timer invoked from: ${from}`); @@ -585,7 +608,7 @@ class Ads { /** * Clear our safety timer(s) - * @param {string} from + * @param {String} from */ clearSafetyTimer(from) { if (!is.nullOrUndefined(this.safetyTimer)) { |