/
player.html
178 lines (161 loc) · 6.16 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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>giggl Player</title>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery-tubeplayer/2.1.0/jquery.tubeplayer.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.2/socket.io.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/annyang/2.6.0/annyang.min.js"></script>
</head>
<body>
<div id='player'></div>
<script>
jQuery(document).ready(function () {
var socket = io();
var current = null;
var playing = false;
var speechVoice = 0;
window.utterances = [];
jQuery("#player").tubeplayer({
autoPlay: false,
onPlayerEnded: function () {
socket.emit('finished', true);
},
onPlayerLoaded: function () {
socket.emit('get', true);
},
onPlayerCued: function () {
jQuery("#player").tubeplayer("pause");
}
});
socket.on('play', function (id) {
playing ? jQuery("#player").tubeplayer('play', id) :
jQuery("#player").tubeplayer('cue', id);
current = id;
});
socket.on('current', function (song) {
if (song !== null && song.id !== current) {
playing ? jQuery("#player").tubeplayer('play', song.id) :
jQuery("#player").tubeplayer('cue', song.id);
} else if (song === null) {
current = null;
jQuery("#player").tubeplayer("pause");
}
});
socket.on('playing', function (bool) {
playing = bool;
if (bool === true) {
jQuery("#player").tubeplayer("play");
} else {
jQuery("#player").tubeplayer("pause");
}
});
socket.on('volume', function (value) {
jQuery("#player").tubeplayer("volume", value);
});
socket.on('current', function (song) {
if (song !== null && song.id !== current) {
jQuery("#player").tubeplayer('play', song.id);
current = song.id;
}
});
if (annyang) {
var play = function () {
if (current !== null) {
if (!playing) speak("Ok");
jQuery("#player").tubeplayer("play");
socket.emit('playing', true);
} else {
speak("I have nothing to play.")
}
};
var pause = function () {
if (playing) speak("Ok");
jQuery("#player").tubeplayer("pause");
socket.emit('playing', false);
};
var skip = function () {
if (current !== null) speak("Ok");
socket.emit('next', true);
};
var hello = function () {
speak("Hello!");
};
var bye = function () {
speak("Bye!");
};
var okay = function () {
speak("What?");
};
var listenTo = function (song) {
console.log(song);
$.get('https://www.googleapis.com/youtube/v3/search', {
key: 'AIzaSyAx4uk0wyOWLxB3n6YN19BQ_WiBIvjN2YA',
type: 'video',
maxResults: '1',
part: 'id,snippet',
fields: 'items/id,items/snippet/title,items/snippet/description,items/snippet/thumbnails/default,items/snippet/channelTitle',
q: song
}).done(function (data) {
speak("Added " + data.items[0].snippet.title + " to the playlist");
socket.emit("add", "https://www.youtube.com/watch?v=" + data.items[0].id.videoId);
playing = true;
socket.emit('playing', true)
});
};
// Let's define our first command. First the text we expect, and then the function it should call
var commands = {
'play music': play,
'continue music': play,
'stop music': pause,
'pause music': pause,
'play song': play,
'continue song': play,
'stop song': pause,
'pause song': pause,
'skip song': skip,
'hello Sarah': hello,
'hi Sarah': hello,
'hey Sarah': hello,
'listen to *song': listenTo,
'add *song': listenTo,
'bye Sarah': bye,
'goodbye Sarah': bye,
'okay Sarah': okay
};
// Add our commands to annyang
annyang.addCommands(commands);
// Start listening. You can call this here, or attach this call to an event, button, etc.
annyang.start();
}
if ('speechSynthesis' in window) {
speechSynthesis.getVoices().forEach(function (voice, index) {
if (voice.name === "Google UK English Female") speechVoice = index;
});
}
function speak(text) {
if ('speechSynthesis' in window) {
if (speechVoice === 0) {
speechSynthesis.getVoices().forEach(function (voice, index) {
if (voice.name === "Google UK English Female") speechVoice = index;
});
}
var msg = new SpeechSynthesisUtterance();
var voices = speechSynthesis.getVoices();
msg.voice = voices[speechVoice];
msg.rate = 1;
msg.pitch = 1;
msg.text = text;
msg.onend = function (e) {
console.log('Finished in ' + event.elapsedTime + ' seconds.');
};
console.log(text);
utterances.push(msg);
speechSynthesis.speak(msg);
}
}
});
</script>
</body>
</html>