-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
247 lines (201 loc) · 8.83 KB
/
index.html
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
236
237
238
239
240
241
242
243
244
245
246
247
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<link href="http://vjs.zencdn.net/6.6.3/video-js.css" rel="stylesheet">
<link rel="stylesheet" href="videojs.ads.css">
<!-- If you'd like to support IE8 -->
<script src="http://vjs.zencdn.net/ie8/1.1.2/videojs-ie8.min.js"></script>
<style>
.video-js{
position: absolute;
}
.video-js.vjs-playing {
z-index: 1;
}
</style>
</head>
<body>
<video id="vid_youtube" class="video-js vjs-default-skin" width="640" height="264" preload="auto" data-setup='{ "techOrder": ["youtube"], "youtube": { "disablekb": 1 }, "youtube": { "iv_load_policy": 3 } }'>
</video>
<video id="examplePlayer" class="video-js" width="640" height="264" preload="auto" controls data-setup="{}">
</video>
<video id="exampleAdContainer" class="video-js ad-container vjs-hidden" width="640" height="264" preload="auto" controls>
</video>
<script src="video.js"></script>
<script src="videojs-playlist.js"></script>
<script src="media.youtube.js"></script>
<script src="videojs.ads.js"></script>
<script>
videojs('vid_youtube').ready(function () {
var player = this;
var adType = 'midroll';
var playerVideoType = 'youtube';
var isPlaylist = true;
if (playerVideoType === 'youtube' && !isPlaylist) {
player.autoplay(true);
player.src([{ "type": "video/youtube", "src": "https://www.youtube.com/watch?v=1I-3vJSC-Vo" }]);
} else if (playerVideoType === 'mp4' && !isPlaylist) {
player.autoplay(true);
player.src([{ "type": "video/mp4", "src": "http://media.w3.org/2010/05/bunny/trailer.mp4" }]);
} else if (playerVideoType === 'youtube' && isPlaylist) {
var samplePlaylist = [{
sources: [{
src: 'https://www.youtube.com/watch?v=1I-3vJSC-Vo',
type: 'video/youtube'
}]
}, {
sources: [{
src: 'https://www.youtube.com/watch?v=T99eO41xwqE',
type: 'video/youtube',
}]
}];
var lastVisitIndex = localStorage.getItem('lastVisitIndex') !== null ? parseFloat(localStorage.getItem('lastVisitIndex')) : 0;
var lastVisitTime = localStorage.getItem('lastVisitTime') !== null ? parseFloat(localStorage.getItem('lastVisitTime')) : 0;
player.playlist(samplePlaylist, lastVisitIndex);
player.playlist.autoadvance(0);
player.playlist.repeat(true);
player.autoplay(true);
player.currentTime(lastVisitTime);
} else if (playerVideoType === 'mp4' && isPlaylist) {
var samplePlaylist = [{
sources: [{
src: 'http://media.w3.org/2010/05/bunny/trailer.mp4',
type: 'video/mp4'
}],
poster: 'http://media.w3.org/2010/05/bunny/poster.png'
}, {
sources: [{
src: 'http://media.w3.org/2010/05/sintel/trailer.mp4',
type: 'video/mp4',
}],
poster: 'http://media.w3.org/2010/05/sintel/poster.png'
}];
var lastVisitIndex = localStorage.getItem('lastVisitIndex') !== null ? parseFloat(localStorage.getItem('lastVisitIndex')) : 0;
var lastVisitTime = localStorage.getItem('lastVisitTime') !== null ? parseFloat(localStorage.getItem('lastVisitTime')) : 0;
player.playlist(samplePlaylist, lastVisitIndex);
player.playlist.autoadvance(0);
player.playlist.repeat(true);
player.autoplay(true);
player.currentTime(lastVisitTime);
}
var adContainer = videojs('exampleAdContainer');
var originalSrc = player.currentSrc();
var state = {};
var midrollPoint = 30;
var playPreroll = true;
var playMidroll = true;
var playPostroll = true;
var seconds = 0;
setInterval(function () {
seconds += 1;
}, 1000);
var requestAds = function () {
// reset plugin state
state = {};
state.inventory = [
{
src: 'kitteh.mp4',
type: 'video/mp4'
}
];
player.trigger('adsready');
if (adType === 'midroll') seconds = 0;
};
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();
}
};
// 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[0];
switchMode('ad', media);
};
// initialize the ads plugin, passing in any relevant options
player.ads({
debug: true
});
// 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);
if (adType === 'midroll') seconds = 0;
});
// 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();
// }
// player.trigger('adsready');
});
if (adType === 'preroll') {
// play an ad the first time there's a preroll opportunity
player.on('readyforpreroll', function () {
playAd();
});
}
if (adType === 'midroll') {
// 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 = seconds;
var opportunity;
if ('lastTime' in state && seconds === midrollPoint) {
opportunity = true;
}
state.lastTime = currentTime;
if (opportunity && playMidroll) {
state.midrollPlayed = false;
playAd();
}
});
}
player.on('play', function () {
if (seconds > midrollPoint && adType === 'midroll') seconds = midrollPoint;
});
});
</script>
</body>
</html>