-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudio.js
executable file
·507 lines (368 loc) · 14.1 KB
/
audio.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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
//audio.js
//song object:
// { src: src, track: track, title: title, artist: artist,
// album: album, album_artist: album_artist }
var currentSong = 0; //playlist index of the currently playing song
var audioPlayer; //primary audio player object for playing music
var audioPreloader; //second audio player object for preloading music
var playhead; //range input object used as the playhead
var volume; //range input object used as the volume slider
var mute; //checkbox used to mute the audio
var playlist = []; //mutable array of song objects in play queue
var browserFrame; //stores a reference to the browser frame
function initPlayer() {
//initializes objects to point to the appropriate html elements
//and adds event listeners for audio players
audioPlayer = document.getElementById('audioPlayer1');
audioPreloader = document.getElementById('audioPlayer2');
playhead = document.getElementById('playhead');
volume = document.getElementById('volume');
mute = document.getElementById('mute');
browserFrame = parent.document.getElementById('browser').contentWindow;
audioPlayer.addEventListener('ended', auto_advance);
audioPlayer.addEventListener('pause', set_play_icon);
audioPlayer.addEventListener('play', set_pause_icon);
audioPlayer.addEventListener('durationchange', set_duration);
audioPreloader.addEventListener('ended', auto_advance);
audioPreloader.addEventListener('pause', set_play_icon);
audioPreloader.addEventListener('play', set_pause_icon);
audioPreloader.addEventListener('durationchange', set_duration);
playhead.addEventListener('input', move_playhead);
volume.addEventListener('input', set_volume);
mute.addEventListener('change', set_mute);
setInterval(update_playhead, 1000);
set_volume();
set_mute();
}
function play_pause() {
//plays or pauses the audioPlayer depending on its current state
if (playlist.length > 0) {
if (audioPlayer.readyState == 0) {
play(0);
} else if (audioPlayer.paused) {
audioPlayer.play();
} else {
audioPlayer.pause();
}
}
}
function previous() {
//plays the previous song in the playlist
if (playlist.length > 0) {
if (currentSong == 0) {
play(playlist.length - 1);
} else {
play(currentSong - 1);
}
}
}
function next() {
//plays the next song in the playlist
if (playlist.length > 0) {
if (currentSong == playlist.length - 1) {
play(0);
} else {
play(currentSong + 1);
}
}
}
function shuffle() {
//shuffles remaining items in the playlist
var played = playlist.slice(0, currentSong + 1);
var remaining = playlist.slice(currentSong + 1);
remaining.sort(function(a, b){return 0.5 - Math.random()});
playlist = played.concat(remaining);
updateCurrent();
updateList();
}
function addSong(song) {
//adds a song object to the playlist
playlist.push(song);
updateList();
if (audioPlayer.paused)
play(playlist.length - 1);
}
function addAlbum(album) {
//adds an album to the playlist
//album is an array or dictionary of song objects
first = playlist.length;
for (var index in album)
playlist.push(album[index]);
updateList();
if (audioPlayer.paused)
play(first);
}
function addArtist(artist) {
//adds an artist to the playlist
//artist is an array or dictionary of albums that
//are arrays or dictionaries of of song objects
first = playlist.length;
for (var album in artist)
for (var index in artist[album])
playlist.push(artist[album][index]);
updateList();
if (audioPlayer.paused)
play(first);
}
function deleteItem(index) {
//removes the song at index from the playlist
if (index == currentSong) {
if (playlist.length == 1) {
clearPlaylist();
} else {
next();
}
}
playlist.splice(index,1);
updateCurrent();
updateInfo();
updateList();
}
function moveUp(index) {
//move the song at index in the playlist up one position
if (index > 0) {
var tmp = playlist[index - 1];
playlist[index - 1] = playlist[index];
playlist[index] = tmp;
updateCurrent();
updateList();
}
}
function moveDown(index) {
//move the song at index in the playlist down one position
if (index < (playlist.length - 1)) {
var tmp = playlist[index + 1];
playlist[index + 1] = playlist[index];
playlist[index] = tmp;
updateCurrent();
updateList();
}
}
function clearPlaylist() {
//resets the playlist, players, and controls to their default, empty states
playlist = [];
audioPlayer.src = '';
audioPreloader.src = '';
document.getElementById('playlistName').value = '';
document.getElementById('currentTime').innerHTML = '--:--';
document.getElementById('duration').innerHTML = '--:--';
playhead.value = 0;
playhead.max = 0;
updateInfo();
updateList();
}
function savePlaylist() {
//saves the current playlist to a file using the name in the playlistName element
var playlistName = encodeURIComponent(document.getElementById('playlistName').value);
var playlistStr = encodeURIComponent(JSON.stringify(playlist));
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
parent.document.getElementById('browser').contentWindow.loadPlaylists();
}
};
xhttp.open('POST', 'saveplaylist.php', true);
xhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhttp.send('name='+playlistName+'&pstr='+playlistStr);
}
function play(index) {
//swaps the audioPlayer and audioPreloader if the song has been preloaded,
//then plays the song at index in the playlist and preloads the next song
if (arguments.length == 1) {
currentSong = index;
if (encodeURI(playlist[currentSong].src) == audioPreloader.src.substr(audioPreloader.src.indexOf('/jones')+6)) {
audioPlayer.pause();
audioPlayer.currentTime = 0;
var tmp = audioPlayer;
audioPlayer = audioPreloader;
audioPreloader = tmp;
audioPlayer.play();
} else {
audioPlayer.src = '/jones'+playlist[currentSong].src;
audioPlayer.play();
}
if (currentSong + 1 < playlist.length)
audioPreloader.src = '/jones'+playlist[currentSong + 1].src;
updateInfo();
updateList();
set_duration();
}
}
function updateInfo() {
//updates the information paragraph with the data from the current song
if (playlist[currentSong] == undefined) {
info = '<b>Now Playing</b><br>'+
'<u>Track</u>: <br>'+
'<u>Title</u>: <br>'+
'<u>Artist</u>: <br>'+
'<u>Album</u>: <br>';
} else {
var song = playlist[currentSong];
var info = '<b>Now Playing</b><br>'+
'<u>Track</u>: '+song.track+'<br>'+
'<u>Title</u>: '+song.title+'<br>'+
'<u>Artist</u>: <a href="javascript:void(0)" onclick="browserFrame.showArtist(\''+song.album_artist+'\')">'+song.artist+'</a><br>'+
'<u>Album</u>: <a href="javascript:void(0)" onclick="browserFrame.showAlbum(\''+song.album_artist+'\',\''+song.album+'\')">'+song.album+'</a><br>';
}
document.getElementById('nowPlayingInfo').innerHTML = info;
}
function updateList() {
//updates the table containing the list of song in the playlist
/*var list = '<colgroup>'+
'<col style="width:12px">'+
'<col style="width:38px">'+
'<col style="width:38px">'+
'<col style="width:38px">'+
'<col>'+
'</colgroup>';*/
var list = '';
for (var i = 0; i < playlist.length; i++) {
list += '<li id="p'+i+'" draggable="true" ondragstart="drag(event)" ondrop="drop(event)" ondragover="dragover(event)" ondragenter="dragenter(event)" ondragleave="dragleave(event)"'+
(i == currentSong ? ' class="current-song"' : '')+'>'+
'<button class="ibtn" onclick="deleteItem('+i+')">X</button> '+
'<a href="javascript:void(0)" onclick="play('+i+')">'+playlist[i].title+'</a></li>';
/*list += '<tr id="p'+i+'" draggable="true" ondragstart="drag(event)" ondrop="drop(event)" ondragover="dragover(event)"><td id="p'+i+'"> | </td>'+
'<td id="p'+i+'"><button class="ibtn" onclick="deleteItem('+i+')">X</button></td>'+
'<td id="p'+i+'"><button class="ibtn" onclick="moveUp('+i+')">▲</button></td>'+
'<td id="p'+i+'"><button class="ibtn" onclick="moveDown('+i+')">▼</button></td>'+
'<td id="p'+i+'"'+(i == currentSong ? ' class="current-song"' : '')+'><a id="p'+i+'" href="javascript:void(0)" onclick="play('+i+')">'+playlist[i].title+'</a></td></tr>';*/
}
/*list += '<tr id="p'+playlist.length+'" ondrop="drop(event)" ondragover="dragover(event)">'+
'<td id="p'+i+'" style="height:24px"></td><td id="p'+i+'" style="height:24px"></td>'+
'<td id="p'+i+'" style="height:24px"></td><td id="p'+i+'" style="height:24px"></td>'+
'<td id="p'+i+'" style="height:24px"></td></tr>';*/
list += '<li id="p'+playlist.length+'" ondrop="drop(event)" ondragover="dragover(event)" ondragenter="dragenter(event)" ondragleave="dragleave(event)">';
document.getElementById('playlist').innerHTML = list;
}
function updateCurrent() {
//sets currentSong to the index of playlist item
//that has the same src as the audioPlayer
var curSrc = audioPlayer.src;
currentSong = -1;
for (var i = 0; i < playlist.length; i++) {
if (encodeURI(playlist[i].src) == curSrc.substr(curSrc.indexOf('/jones')+6)) {
currentSong = i;
break;
}
}
}
function formatTime(s) {
//converts seconds to a string of minutes:seconds
if (isNaN(s)) {
return "--:--";
} else {
s = Math.round(s);
m = Math.floor(s / 60);
s = s % 60;
m = m < 10 ? 0 + String(m) : String(m);
s = s < 10 ? 0 + String(s) : String(s);
return m + ":" + s;
}
}
function auto_advance() {
//plays the next song with the song ends
//repeats the playlist if repeat is checked
//called by the audioPlayer ended event listener
if ( ( currentSong == (playlist.length - 1) ) &&
( document.getElementById('repeat').checked) ) {
play(0);
} else {
play(currentSong + 1);
}
}
function set_pause_icon() {
//sets the play/pause icon to show a pause symbol
//called by the audioPlayer play event listener
document.getElementById('play_pause').innerHTML = '<b>||</b>';
}
function set_play_icon() {
//sets the play/pause icon to show a play symbol
//called by the audioPlayer pause event listener
document.getElementById('play_pause').innerHTML = '►';
}
function set_duration() {
//updates the duration label to reflect the current song
//called by the audioPlayer durationchange event listener
playhead.max = isNaN(audioPlayer.duration) ? 0 : audioPlayer.duration;
document.getElementById('duration').innerHTML = formatTime(audioPlayer.duration);
}
function move_playhead() {
//sets the playback position of the audioPlayer and the currentTime label
//based on the user interaction with the playhead
//called by the playhead input event listener
audioPlayer.currentTime = playhead.value;
document.getElementById('currentTime').innerHTML = formatTime(audioPlayer.currentTime);
}
function update_playhead() {
//updates the position of the playhead and the currentTime label
//called on an interval
if (!audioPlayer.paused) {
playhead.value = audioPlayer.currentTime;
document.getElementById('currentTime').innerHTML = formatTime(audioPlayer.currentTime);
}
}
function set_volume() {
//sets the volume of the audioPlayer and the audioPreloader
//based on the user interaction with the volume slider
//called by the volume input event listener
audioPlayer.volume = audioPreloader.volume = volume.value / 100.0;
}
function set_mute() {
//sets the mute state of the audioPlayer and the audioPreloader
//based on the user interaction with the volume slider
//called by the muted change event listener
audioPlayer.muted = audioPreloader.muted = volume.disabled = mute.checked;
}
function drag(e) {
//drag function for playlist row elements
//records the id of the dragged item
e.dataTransfer.setData("text/plain", e.target.id);
}
function drop(e) {
//drop function for playlist row elements
//sends the indices of the dragged element
//and the one it was dropped on to the move function
e.preventDefault();
var from = parseInt(e.dataTransfer.getData("text/plain").substring(1))
var to = parseInt(e.target.id.substring(1))
move(from, to);
}
function dragover(e) {
//dragover function for playlist row elements
e.preventDefault();
}
function dragover(e) {
//dragover function for playlist row elements
e.preventDefault();
}
function dragenter(e) {
e.target.style.borderTop = "1px solid black";
}
function dragleave(e) {
e.target.style.borderTop = "1px solid white";
}
function move(from, to) {
//places the playlist item at the from index above the
//item at the to index
if (from < to && to < playlist.length) {
p1 = playlist.slice(0, from);
p2 = playlist.slice(from + 1, to);
p3 = playlist.slice(to);
playlist = p1.concat(p2, [playlist[from]], p3);
} else if (to < from && from < playlist.length - 1) {
p1 = playlist.slice(0, to);
p2 = playlist.slice(to, from);
p3 = playlist.slice(from + 1);
playlist = p1.concat([playlist[from]], p2, p3);
} else if (from < to && to == playlist.length) {
p1 = playlist.slice(0, from);
p2 = playlist.slice(from + 1);
playlist = p1.concat(p2, [playlist[from]]);
} else if (to < from && from == playlist.length - 1) {
p1 = playlist.slice(0, to);
p2 = playlist.slice(to, from);
playlist = p1.concat([playlist[from]], p2);
}
updateCurrent();
updateList();
}