-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.js
405 lines (358 loc) · 11.1 KB
/
player.js
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/*!
* Howler.js Audio Player Demo
* howlerjs.com
*
* (c) 2013-2019, James Simpson of GoldFire Studios
* goldfirestudios.com
*
* MIT License
*/
// Cache references to DOM elements.
var elms = ['track', 'timer', 'duration', 'playBtn', 'pauseBtn', 'prevBtn', 'nextBtn', 'playlistBtn', 'volumeBtn', 'progress', 'bar', 'wave', 'loading', 'playlist', 'list', 'volume', 'barEmpty', 'barFull', 'sliderBtn'];
elms.forEach(function(elm) {
window[elm] = document.getElementById(elm);
});
/**
* Player class containing the state of our playlist and where we are in it.
* Includes all methods for playing, skipping, updating the display, etc.
* @param {Array} playlist Array of objects with playlist song details ({title, file, howl}).
*/
var Player = function(playlist) {
this.playlist = playlist;
this.index = 0;
// Display the title of the first track.
track.innerHTML = '1. ' + playlist[0].title;
// Setup the playlist display.
playlist.forEach(function(song) {
var div = document.createElement('div');
div.className = 'list-song';
div.innerHTML = song.title;
div.onclick = function() {
player.skipTo(playlist.indexOf(song));
};
list.appendChild(div);
});
};
Player.prototype = {
/**
* Play a song in the playlist.
* @param {Number} index Index of the song in the playlist (leave empty to play the first or current).
*/
play: function(index) {
var self = this;
var sound;
index = typeof index === 'number' ? index : self.index;
var data = self.playlist[index];
// If we already loaded this track, use the current one.
// Otherwise, setup and load a new Howl.
if (data.howl) {
sound = data.howl;
} else {
sound = data.howl = new Howl({
src: ['./audio/' + data.file + '.webm', './audio/' + data.file + '.mp3'],
html5: true, // Force to HTML5 so that the audio can stream in (best for large files).
onplay: function() {
fetch('http://localhost:5000/collect', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({
title: data.title,
type: 'PLAY',
id: uuidv4()
})
});
// Display the duration.
duration.innerHTML = self.formatTime(Math.round(sound.duration()));
// Start upating the progress of the track.
requestAnimationFrame(self.step.bind(self));
// Start the wave animation if we have already loaded
wave.container.style.display = 'block';
bar.style.display = 'none';
pauseBtn.style.display = 'block';
},
onload: function() {
// Start the wave animation.
wave.container.style.display = 'block';
bar.style.display = 'none';
loading.style.display = 'none';
},
onend: function() {
fetch('http://localhost:5000/collect', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({
title: data.title,
type: 'FINISH',
id: uuidv4()
})
});
// Stop the wave animation.
wave.container.style.display = 'none';
bar.style.display = 'block';
self.skip('next');
},
onpause: function() {
// Stop the wave animation.
wave.container.style.display = 'none';
bar.style.display = 'block';
},
onstop: function() {
// Stop the wave animation.
wave.container.style.display = 'none';
bar.style.display = 'block';
},
onseek: function() {
// Start upating the progress of the track.
requestAnimationFrame(self.step.bind(self));
}
});
}
// Begin playing the sound.
sound.play();
// Update the track display.
track.innerHTML = (index + 1) + '. ' + data.title;
// Show the pause button.
if (sound.state() === 'loaded') {
playBtn.style.display = 'none';
pauseBtn.style.display = 'block';
} else {
loading.style.display = 'block';
playBtn.style.display = 'none';
pauseBtn.style.display = 'none';
}
// Keep track of the index we are currently playing.
self.index = index;
},
/**
* Pause the currently playing track.
*/
pause: function() {
var self = this;
// Get the Howl we want to manipulate.
var sound = self.playlist[self.index].howl;
// Puase the sound.
sound.pause();
// Show the play button.
playBtn.style.display = 'block';
pauseBtn.style.display = 'none';
},
/**
* Skip to the next or previous track.
* @param {String} direction 'next' or 'prev'.
*/
skip: function(direction) {
var self = this;
// Get the next track based on the direction of the track.
var index = 0;
if (direction === 'prev') {
index = self.index - 1;
if (index < 0) {
index = self.playlist.length - 1;
}
} else {
index = self.index + 1;
if (index >= self.playlist.length) {
index = 0;
}
}
self.skipTo(index);
},
/**
* Skip to a specific track based on its playlist index.
* @param {Number} index Index in the playlist.
*/
skipTo: function(index) {
var self = this;
// Stop the current track.
if (self.playlist[self.index].howl) {
self.playlist[self.index].howl.stop();
}
// Reset progress.
progress.style.width = '0%';
// Play the new track.
self.play(index);
},
/**
* Set the volume and update the volume slider display.
* @param {Number} val Volume between 0 and 1.
*/
volume: function(val) {
var self = this;
// Update the global volume (affecting all Howls).
Howler.volume(val);
// Update the display on the slider.
var barWidth = (val * 90) / 100;
barFull.style.width = (barWidth * 100) + '%';
sliderBtn.style.left = (window.innerWidth * barWidth + window.innerWidth * 0.05 - 25) + 'px';
},
/**
* Seek to a new position in the currently playing track.
* @param {Number} per Percentage through the song to skip.
*/
seek: function(per) {
var self = this;
// Get the Howl we want to manipulate.
var sound = self.playlist[self.index].howl;
// Convert the percent into a seek position.
if (sound.playing()) {
sound.seek(sound.duration() * per);
}
},
/**
* The step called within requestAnimationFrame to update the playback position.
*/
step: function() {
var self = this;
// Get the Howl we want to manipulate.
var sound = self.playlist[self.index].howl;
// Determine our current seek position.
var seek = sound.seek() || 0;
timer.innerHTML = self.formatTime(Math.round(seek));
progress.style.width = (((seek / sound.duration()) * 100) || 0) + '%';
// If the sound is still playing, continue stepping.
if (sound.playing()) {
requestAnimationFrame(self.step.bind(self));
}
},
/**
* Toggle the playlist display on/off.
*/
togglePlaylist: function() {
var self = this;
var display = (playlist.style.display === 'block') ? 'none' : 'block';
setTimeout(function() {
playlist.style.display = display;
}, (display === 'block') ? 0 : 500);
playlist.className = (display === 'block') ? 'fadein' : 'fadeout';
},
/**
* Toggle the volume display on/off.
*/
toggleVolume: function() {
var self = this;
var display = (volume.style.display === 'block') ? 'none' : 'block';
setTimeout(function() {
volume.style.display = display;
}, (display === 'block') ? 0 : 500);
volume.className = (display === 'block') ? 'fadein' : 'fadeout';
},
/**
* Format the time from seconds to M:SS.
* @param {Number} secs Seconds to format.
* @return {String} Formatted time.
*/
formatTime: function(secs) {
var minutes = Math.floor(secs / 60) || 0;
var seconds = (secs - minutes * 60) || 0;
return minutes + ':' + (seconds < 10 ? '0' : '') + seconds;
}
};
// Setup our new audio player class and pass it the playlist.
var player = new Player([
{
title: 'Rave Digger',
file: 'rave_digger',
howl: null
},
{
title: '80s Vibe',
file: '80s_vibe',
howl: null
},
{
title: 'Running Out',
file: 'running_out',
howl: null
}
]);
// Bind our player controls.
playBtn.addEventListener('click', function() {
player.play();
});
pauseBtn.addEventListener('click', function() {
player.pause();
});
prevBtn.addEventListener('click', function() {
player.skip('prev');
});
nextBtn.addEventListener('click', function() {
player.skip('next');
});
waveform.addEventListener('click', function(event) {
player.seek(event.clientX / window.innerWidth);
});
playlistBtn.addEventListener('click', function() {
player.togglePlaylist();
});
playlist.addEventListener('click', function() {
player.togglePlaylist();
});
volumeBtn.addEventListener('click', function() {
player.toggleVolume();
});
volume.addEventListener('click', function() {
player.toggleVolume();
});
// Setup the event listeners to enable dragging of volume slider.
barEmpty.addEventListener('click', function(event) {
var per = event.layerX / parseFloat(barEmpty.scrollWidth);
player.volume(per);
});
sliderBtn.addEventListener('mousedown', function() {
window.sliderDown = true;
});
sliderBtn.addEventListener('touchstart', function() {
window.sliderDown = true;
});
volume.addEventListener('mouseup', function() {
window.sliderDown = false;
});
volume.addEventListener('touchend', function() {
window.sliderDown = false;
});
var move = function(event) {
if (window.sliderDown) {
var x = event.clientX || event.touches[0].clientX;
var startX = window.innerWidth * 0.05;
var layerX = x - startX;
var per = Math.min(1, Math.max(0, layerX / parseFloat(barEmpty.scrollWidth)));
player.volume(per);
}
};
volume.addEventListener('mousemove', move);
volume.addEventListener('touchmove', move);
// Setup the "waveform" animation.
var wave = new SiriWave({
container: waveform,
width: window.innerWidth,
height: window.innerHeight * 0.3,
cover: true,
speed: 0.03,
amplitude: 0.7,
frequency: 2
});
wave.start();
// Update the height of the wave animation.
// These are basically some hacks to get SiriWave.js to do what we want.
var resize = function() {
var height = window.innerHeight * 0.3;
var width = window.innerWidth;
wave.height = height;
wave.height_2 = height / 2;
wave.MAX = wave.height_2 - 4;
wave.width = width;
wave.width_2 = width / 2;
wave.width_4 = width / 4;
wave.canvas.height = height;
wave.canvas.width = width;
wave.container.style.margin = -(height / 2) + 'px auto';
// Update the position of the slider.
var sound = player.playlist[player.index].howl;
if (sound) {
var vol = sound.volume();
var barWidth = (vol * 0.9);
sliderBtn.style.left = (window.innerWidth * barWidth + window.innerWidth * 0.05 - 25) + 'px';
}
};
window.addEventListener('resize', resize);
resize();