forked from videojs/videojs-contrib-ads
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathexample-integration.js
More file actions
127 lines (101 loc) · 3.78 KB
/
example-integration.js
File metadata and controls
127 lines (101 loc) · 3.78 KB
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
/**
* 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";
/**
* Register the ad integration plugin.
* To initialize for a player, call player.exampleAds().
*
* @param {mixed} options Hash of obtions for the exampleAds plugin.
*/
vjs.plugin('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
state = {},
// 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", "inventory.json?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) {
return;
}
// tell ads plugin we're ready to play our ad
player.ads.startLinearAdMode();
state.adPlaying = true;
// tell videojs to load the ad
var url = state.inventory[Math.floor(Math.random() * state.inventory.length)];
player.src(url);
// when the video metadata is loaded, play it!
player.one('durationchange', function() {
player.play();
});
// when it's finished
player.one('ended', 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 immediately,
// or whenever the player gets new content to play
if (player.currentSrc()) {
requestAds();
}
player.on('contentupdate', requestAds);
// play an ad the first time there's a preroll opportunity
player.on('readyforpreroll', function() {
if (!state.prerollPlayed) {
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 > 15 && state.lastTime < 15;
}
state.lastTime = currentTime;
if (opportunity) {
state.midrollPlayed = true;
playAd();
}
});
});
})(window, document, videojs);