forked from videojs/videojs-contrib-ads
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample-integration.js
141 lines (113 loc) · 4.5 KB
/
example-integration.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/**
* Example ad integration using the videojs-ads plugin.
*
* For each content video, this plugin plays one preroll and one midroll.
* Ad content is chosen randomly from the URLs listed in inventory.json.
*/
(function(window, document, vjs, undefined) {
"use strict";
var registerPlugin = vjs.registerPlugin || vjs.plugin;
/**
* Register the ad integration plugin.
* To initialize for a player, call player.exampleAds().
*
* @param {mixed} options Hash of obtions for the exampleAds plugin.
*/
registerPlugin('exampleAds', function(options){
var
player = this,
// example plugin state, may have any of these properties:
// - inventory - hypothetical ad inventory, list of URLs to ads
// - lastTime - the last time observed during content playback
// - adPlaying - whether a linear ad is currently playing
// - prerollPlayed - whether we've played a preroll
// - midrollPlayed - whether we've played a midroll
// - postrollPlayed - whether we've played a postroll
state = {},
// just like any other video.js plugin, ad integrations can
// accept initialization options
adServerUrl = (options && options.adServerUrl) || "inventory.json",
midrollPoint = (options && options.midrollPoint) || 15,
playPreroll = options && options.playPreroll !== undefined ? options.playPreroll : true,
playMidroll = options && options.playMidroll !== undefined ? options.playMidroll : true,
playPostroll = options && options.playPostroll !== undefined ? options.playPostroll : true,
// asynchronous method for requesting ad inventory
requestAds = function() {
// reset plugin state
state = {};
// fetch ad inventory
// the 'src' parameter is ignored by the example inventory.json flat file,
// but this shows how you might send player information along to the ad server.
var xhr = new XMLHttpRequest();
xhr.open("GET", adServerUrl + "?src=" + encodeURIComponent(player.currentSrc()));
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
try {
state.inventory = JSON.parse(xhr.responseText);
player.trigger('adsready');
} catch (err) {
throw new Error('Couldn\'t parse inventory response as JSON');
}
}
};
xhr.send(null);
},
// play an ad, given an opportunity
playAd = function() {
// short-circuit if we don't have any ad inventory to play
if (!state.inventory || state.inventory.length === 0) {
videojs.log('No inventory to play.');
return;
}
// tell ads plugin we're ready to play our ad
player.ads.startLinearAdMode();
state.adPlaying = true;
// tell videojs to load the ad
var media = state.inventory[Math.floor(Math.random() * state.inventory.length)];
player.src(media);
// when it's finished
player.one('adended', function() {
// play your linear ad content, then when it's finished ...
player.ads.endLinearAdMode();
state.adPlaying = false;
});
};
// initialize the ads plugin, passing in any relevant options
player.ads(options);
// request ad inventory whenever the player gets new content to play
player.on('contentupdate', requestAds);
// if there's already content loaded, request an add immediately
if (player.currentSrc()) {
requestAds();
}
player.on('contentended', function() {
if (!state.postrollPlayed && player.ads.state === 'postroll?' && playPostroll) {
state.postrollPlayed = true;
playAd();
}
});
// play an ad the first time there's a preroll opportunity
player.on('readyforpreroll', function() {
if (!state.prerollPlayed && playPreroll) {
state.prerollPlayed = true;
playAd();
}
});
// watch for time to pass 15 seconds, then play an ad
// if we haven't played a midroll already
player.on('timeupdate', function(event) {
if (state.midrollPlayed) {
return;
}
var currentTime = player.currentTime(), opportunity;
if ('lastTime' in state) {
opportunity = currentTime > midrollPoint && state.lastTime < midrollPoint;
}
state.lastTime = currentTime;
if (opportunity && playMidroll) {
state.midrollPlayed = true;
playAd();
}
});
});
})(window, document, videojs);