forked from anssiko/requestshowmedia
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathplayer.html
103 lines (85 loc) · 2.96 KB
/
player.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
<!DOCTYPE html>
<html>
<head>
<title>Player</title>
<style>
html { width: 100%; height: 100%; background-color: #000; }
body { margin: 0; height: 100%; width: 100%; display: flex;
align-items: center; justify-content: center; }
video { width: 100%; }
</style>
<script>
(function () {
var openerOrigin = null,
timeUpdateListener = null;
function open(event) {
var video = document.querySelector('video'),
currentSrc = event.data.currentSrc,
currentTime = event.data.currentTime;
timeUpdateListener = function () {
opener.postMessage({
cmd: 'ontimeupdateonsecondscreen',
currentTime: video.currentTime
}, openerOrigin);
};
/*
FIXME: Chrome does not fire loadedmetadata until the
first video has fully loaded.
Issues: https://crbug.com/31014
https://crbug.com/116670
Patch: https://codereview.chromium.org/232003002/
A workaround is to add a random query string to the currentSrc.
*/
video.src = currentSrc + '?' + (new Date()).getTime();
video.dataset.filename = currentSrc.split('/')[currentSrc.split('/').length-1];
document.title = '[Playing] ' + video.dataset.filename;
video.addEventListener('loadedmetadata', function () {
video.currentTime = currentTime;
});
video.addEventListener('timeupdate', timeUpdateListener);
}
addEventListener('message', function (event) {
var video = document.querySelector('video');
openerOrigin = event.origin;
if (video.seeking) return;
switch (event.data.cmd) {
case 'open':
open(event);
break;
case 'play':
video.play();
document.title = '[Playing] ' + video.dataset.filename;
break;
case 'pause':
video.pause();
document.title = '[Paused] ' + video.dataset.filename;
break;
case 'fastSeek':
// video.fastSeek(event.data.currentTime); // not implemented by browsers yet
// ... so we're emulating fastSeek() by setting currentTime
video.currentTime = event.data.currentTime;
break;
default:
break;
}
});
addEventListener('beforeunload', function () {
var video = document.querySelector('video');
// FIXME: remove timeUpdateListener before unload to prevent multiple
// listeners posting the same data if open() is called multiple times.
video.removeEventListener('timeupdate', timeUpdateListener);
opener.postMessage({
cmd: 'close',
}, openerOrigin);
opener.postMessage({
cmd: 'play',
currentTime: video.currentTime
}, openerOrigin);
});
}());
</script>
</head>
<body>
<video autoplay></video>
</body>
</html>