-
Notifications
You must be signed in to change notification settings - Fork 7.4k
/
middleware-play.html.example
152 lines (128 loc) · 4.37 KB
/
middleware-play.html.example
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
<!DOCTYPE html>
<html lang="en-GB">
<head>
<meta charset="utf-8" />
<title>Video.js Sandbox</title>
<!-- Load the source files -->
<link href="../dist/video-js.css" rel="stylesheet" type="text/css">
<script src="../dist/video.js"></script>
<style>
.terminate-btn {
margin: 2em 1em;
}
.terminated .vjs-progress-control .vjs-play-progress {
background: red;
}
</style>
</head>
<body>
<video id="vid1" class="video-js" lang="en" controls poster="//d2zihajmogu5jn.cloudfront.net/elephantsdream/poster.png">
<source src="//d2zihajmogu5jn.cloudfront.net/elephantsdream/ed_hd.mp4" type="video/mp4">
<source src="//d2zihajmogu5jn.cloudfront.net/elephantsdream/ed_hd.ogg" type="video/ogg">
</video>
<input id="stateToggle" type="checkbox" class="terminate-btn">
Terminate the play/pause middleware
</input>
<script>
var stateToggle = document.getElementById('stateToggle');
// Middleware 1
var m1 = function(player) {
return {
// Mediating play to the tech
callPlay: function() {
if (stateToggle.checked) {
console.log('Middleware 1: Play is set to terminate');
player.addClass('terminated');
return videojs.middleware.TERMINATOR;
} else {
console.log('Middleware 1: Play has been called');
player.removeClass('terminated');
}
},
// Mediating the results back to the player
play: function(cancelled, value) {
console.log('Middleware 1: play got from tech. What is the value passed?', value);
// Handle the promise if it is returned
if(value && value.then) {
value.then(() => {
console.log('Middleware 1: Promise resolved.')
})
.catch((err) => {
console.log('Middleware 1: Promise rejected.');
});
}
if (cancelled) {
console.log('Middleware 1: play has been cancelled prior to this middleware');
}
},
// Mediating to tech
callPause: function() {
if (stateToggle.checked) {
console.log('Middleware 1: Pause is set to terminate');
player.addClass('terminated');
return videojs.middleware.TERMINATOR;
} else {
console.log('Middleware 1: Pause has been called');
player.removeClass('terminated');
}
},
// Mediating the results back to the player
pause: function(cancelled, value) {
console.log('Middleware 1: pause got back from tech. What is the value passed?', value);
if (cancelled) {
console.log('Middleware 1: pause has been cancelled prior to this middleware');
}
return value;
},
// Required for middleware. Simply passes along the source
setSource: function(srcObj, next) {
next(null, srcObj);
}
};
};
// Middleware 2
var m2 = function(player) {
return {
callPlay: function() {
console.log('Middleware 2: play has been called');
},
play: function(cancelled, value) {
console.log('Middleware 2: got play from tech. What is the value passed?', value);
if (cancelled) {
console.log('Middleware 2: play has been cancelled prior to this middleware');
}
return value;
},
callPause: function() {
console.log('Middleware 2: pause has been called');
},
pause: function(cancelled, value) {
console.log('Middleware 2: got pause from tech. What is the value passed?', value);
if (cancelled) {
console.log('Middleware 2: pause has been cancelled prior to this middleware');
}
return value;
},
setSource: function(srcObj, next) {
next(null, srcObj);
}
};
}
videojs.use('*', m1);
videojs.use('*', m2);
// Initial set-up
var vid = document.getElementById("vid1");
var player = videojs(vid);
console.log('Calling play...');
player.setTimeout(() => {
player.play()
.then(() => {
console.log('The promise resolved, we are playing.');
},
(err) => {
console.log('The promise was rejected, we failed to play.');
});
}, 500);
</script>
</body>
</html>