videojs-contrib-ads
Version:
A framework that provides common functionality needed by video advertisement libraries working with video.js.
38 lines (30 loc) • 1.09 kB
JavaScript
/*
This feature sends a `contentupdate` event when the player source changes.
*/
// Start sending contentupdate events
const initializeContentupdate = function(player) {
// Keep track of the current content source
// If you want to change the src of the video without triggering
// the ad workflow to restart, you can update this variable before
// modifying the player's source
player.ads.contentSrc = player.currentSrc();
// Check if a new src has been set, if so, trigger contentupdate
const checkSrc = function() {
if (player.ads.state !== 'ad-playback') {
const src = player.currentSrc();
if (src !== player.ads.contentSrc) {
player.trigger({
type: 'contentupdate',
oldValue: player.ads.contentSrc,
newValue: src
});
player.ads.contentSrc = src;
}
}
};
// loadstart reliably indicates a new src has been set
player.on('loadstart', checkSrc);
// check immediately in case we missed the loadstart
window.setTimeout(checkSrc, 1);
};
module.exports = initializeContentupdate;