-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
112 lines (86 loc) · 2.17 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
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div id="player"></div>
<input id="input" type="text" />
<input id="check" type="checkbox" />
<script src="http://www.youtube.com/player_api"></script>
<script src="/socket.io/socket.io.js"></script>
<script>
var player, socket,
input = document.getElementById('input'),
playlist = [];
input.addEventListener('keydown', function (e) {
var val = input.value.trim();
if (e.keyCode === 13) {
socket.emit('newVid', val);
playlist.push(val);
if (!player || player.getPlayerState() !== 1) {
insertNewVid(playlist[0]);
if (document.getElementById('check').checked && player.playVideo) {
player.playVideo();
}
}
input.value = '';
}
});
function onYouTubePlayerAPIReady() {
console.log(location.href);
socket = io.connect(location.href);
socket.on('connect', function () {
console.log('connect');
})
socket.on('init', function (list) {
playlist = list;
console.log('init');
if (playlist[0]) {
insertNewVid(playlist[0]);
}
});
socket.on('newVid', function (vid) {
playlist.push(vid);
if (!player || player.getPlayerState() !== 1) {
insertNewVid(playlist[0]);
if (document.getElementById('check').checked) {
player.playVideo();
}
}
});
socket.on('shiftPlaylist', function (vid) {
playlist.shift();
});
}
function insertNewVid(vidId) {
if (player) {
player.loadVideoById(vidId);
return;
}
player = new YT.Player('player', {
height : '390',
width : '640',
videoId : vidId,
events : {
'onReady' : onPlayerReady,
'onStateChange' : onPlayerStateChange
}
});
}
// autoplay video
function onPlayerReady(event) {
if (document.getElementById('check').checked) {
event.target.playVideo();
}
}
// when video ends
function onPlayerStateChange(event) {
if ( (event.data === 0 ) && playlist[0] ) {
playlist.shift();
socket.emit('shiftPlaylist');
insertNewVid(playlist[0]);
}
}
</script>
</body>
</html>