forked from videojs/videojs-contrib-ads
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcue-text-tracks-example.html
More file actions
235 lines (190 loc) · 6.92 KB
/
cue-text-tracks-example.html
File metadata and controls
235 lines (190 loc) · 6.92 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Example Ad Integration</title>
<!-- Load local video.js -->
<link rel="stylesheet" href="../node_modules/video.js/dist/video-js.css">
<script src="../node_modules/video.js/dist/video.js"></script>
<!-- Load local ads plugin. -->
<link rel="stylesheet" href="../dist/videojs.ads.css">
<script src="../dist/videojs.ads.js"></script>
<!-- Load example ads integraiton. -->
<script src="example-integration.js"></script>
<link rel="stylesheet" href="app.css">
</head>
<body>
<h1>Cue Text Tracks Example</h1>
<p>This page uses the built files, so you must do a local build before the
example will work.</p>
<video
id="examplePlayer"
class="video-js"
width="640"
height="264"
poster="http://vjs.zencdn.net/v/oceans.png"
preload="auto"
controls
data-setup="{}">
<source src="http://vjs.zencdn.net/v/oceans.mp4" type='video/mp4' />
<source src="http://vjs.zencdn.net/v/oceans.webm" type='video/webm' />
<source src="http://vjs.zencdn.net/v/oceans.ogv" type='video/ogg' />
</video>
<video
id="exampleAdContainer"
class="video-js ad-container vjs-hidden"
width="640"
height="264"
preload="auto"
controls>
</video>
<h2>Cue Text Tracks</h2>
<p>
During ads, the control bar will be <font color="red">red</font>.
Open the developer console to view logs.
</p>
<script>
videojs('examplePlayer').ready(function() {
var player = this;
var adContainer = videojs('exampleAdContainer');
var adServerUrl = "inventory.json";
var originalSrc = player.currentSrc();
var state = {};
var playedCues = {};
// asynchronous method for requesting ad inventory
var requestAds = function() {
videojs.log('example', 'Requesting an ad');
// 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);
};
var switchMode = function(mode, src) {
// switch to ad mode
if (mode === 'ad') {
videojs.log('example', 'Going into ad mode with', src.src);
// pause content immediately
player.pause();
// show ad container, hide content player
adContainer.src(src);
adContainer.removeClass('vjs-hidden');
player.addClass('vjs-hidden');
// play ad
adContainer.play();
} else if (mode === 'content') {
videojs.log('example', 'Going into content mode with', src);
// hide ad container, show content player
player.removeClass('vjs-hidden');
adContainer.addClass('vjs-hidden');
// resume content
player.play();
}
};
var cueChangeHandler = function(event) {
if (state.adPlaying || !this.activeCues || this.activeCues.length === 0) {
return;
}
var activeCue = this.activeCues[this.activeCues.length - 1];
var cues = [activeCue];
videojs.log('example', 'A cue change fired at', player.currentTime(),
'cue is', cues[0], 'at', cues[0].startTime);
// Make an ad request
var processCue = function(player, cue, cueId, startTime) {
if (cue.text != 'ad' || player.paused() ||
state.adPlaying || playedCues[cueId]) {
return;
}
playAd();
playedCues[cueId] = true;
};
// Optional method to dynamically cancel ads
var cancelAds = function(player, cue) {
if (cue.text === 'adCancel' && state.adPlaying) {
state.adPlaying = false;
player.ads.endLinearAdMode();
switchMode('content', originalSrc);
}
};
// Process the cues in this track as ad cues using `processCue`
// Cancel ads dynamically using `cancelAds`
player.ads.cueTextTracks.processAdTrack(player, cues, processCue, cancelAds);
};
// play an ad, given an opportunity
var playAd = function() {
// short-circuit if we don't have any ad inventory to play
if (!state.inventory || state.inventory.length === 0) {
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)];
switchMode('ad', media);
};
var processMetadataTrack = function(player, track) {
track.addEventListener('cuechange', cueChangeHandler);
}
// initialize the ads plugin, passing in any relevant options
player.ads({
debug: true
});
// Set up to process metadata cues
player.ads.cueTextTracks.processMetadataTracks(player, processMetadataTrack);
// Set up a base text track to use for our ad cuepoints
player.addRemoteTextTrack({
kind: 'metadata',
src: './testcuepoint.vtt'
});
// indicate that there will be no preroll and postroll
player.trigger('nopreroll');
player.trigger('nopostroll');
// Set up to return from ads
adContainer.on('ended', function(e) {
if (!state.adPlaying) {
return;
}
// play your linear ad content, then when it's finished ...
player.ads.endLinearAdMode();
state.adPlaying = false;
switchMode('content', originalSrc);
// re-enable the text track
player.textTracks()[0].mode = 'hidden';
});
// request ad inventory whenever the player gets new content to play
if (player.currentSrc() && player.paused()) {
requestAds();
}
// Implement an interface for understanding the adCues
player.ads.cueTextTracks.setMetadataTrackMode = function(track) {
// The cues are invisible, so set them to hidden so they can be updated
track.mode = 'hidden';
};
player.ads.cueTextTracks.getSupportedAdCue = function(player, cue) {
// Make sure this is an ad cue
if (cue.text === 'ad' || cue.text === 'adCancel') {
return cue;
}
return -1;
};
player.ads.cueTextTracks.getCueId = function (player, cue) {
// Return the topmost id
return Number(cue.id);
};
});
</script>
</body>