forked from antimatter15/player
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.html
524 lines (464 loc) · 14.6 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
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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
<!doctype html>
<html>
<head>
<title>MP3 Player</title>
<link rel="icon" type="image/png" href="16.png">
<style>
body {
background: #444;
background-image: -webkit-gradient(
linear,
left top,
right top,
color-stop(0.8, rgb(133,133,133)),
color-stop(1, rgb(68,68,68))
);
background-image: -moz-linear-gradient(
left center,
rgb(133,133,133) 80%,
rgb(68,68,68) 100%
);
font-family: sans-serif;
font-size: 14px;
margin: 0;
overflow-y: scroll;
}
#startup #prompt {
width: 200px;
background: #ddd;
height: 20%;
width: 30%;
min-height: 180px;
border-radius: 5px;
z-index: 100;
position: absolute;
top: 35%;
left: 35%;
}
#mask {
position: absolute;
opacity: 0.5;
width: 100%;
left: 0;
top: 0;
height: 100%;
z-index: 4;
background: #000;
}
#prompt input {
margin-top: 15px;
margin-left: 15px;
}
#search {
width: 80%;
position:fixed;
top: 0;
padding:0;
margin:0;
}
#search input {
width: 100%;
height: 36px;
font-size: 30px;
border: none;
padding-left: 5px;
border-bottom: 1px solid #929292;
outline: none;
background: #929292;
background-image: -webkit-gradient(
linear,
left top,
left bottom,
color-stop(0, rgb(255,255,255)),
color-stop(1, rgb(221,221,221))
);
background-image: -moz-linear-gradient(
center top,
rgb(255,255,255) 0%,
rgb(221,221,221) 100%
);
}
audio {
width: 100%;
}
#footer {
position: fixed;
bottom: -3px;
width: 100%;
background: #858585;
}
#songs {
background: #ddd;
width: 80%;
padding-left: 5px;
}
#songs table {
width: 100%;
}
#playlist {
width: 20%;
position: fixed;
right: 0;
color: white;
top: 0;
}
#playlist table {
width: 100%;
margin: 5px;
margin-left: 10px;
}
table tr:hover {
background: #BBCEE9;
border-radius: 5px;
}
table tr {
-webkit-user-select: none;
user-select: none;
cursor: default;
}
table td {
padding-top: 3px;
padding-bottom: 3px;
padding-right: 5px;
}
#playlist table tr.playing {
/*background: #76AD8B;*/
font-style: italic;
}
#playlist table tr.playing td {
padding-left: 20px;
}
#listtools {
position: fixed;
bottom: 40px;
right: 5px;
color: #ddd;
}
#listtools a {
color: white;
text-decoration: none;
}
tr.hidden {
display:none;
}
</style>
<script>
//beware! this code is pretty hacky and ugly.
/*
This doesn't use jQuery or any js libraries, not because they aren't great
in retrospect, I really think I should have used jQuery here.
But yesterday when I started this, I was really offended by a chrome extension
which was literally a one liner content script that had jquery as a dependency.
That's just terrible.
Anyway, this is more of a proof of concept and in a weird backwards way, I prefer
to do my prototypes without jQuery.
*/
function runSearch(query){
console.log(query);
var regex = new RegExp(query.trim().replace(/\s+/g, '.*'), 'ig');
for(var i = $('songtable').getElementsByTagName('tr'), l = i.length; l--;){
if(regex.test(i[l].innerHTML)){
i[l].className = 'visible'
}else{
i[l].className = 'hidden';
}
}
}
function canPlay(type){
var a = document.createElement('audio');
return !!(a.canPlayType && a.canPlayType(type).replace(/no/, ''));
}
function $(id){return document.getElementById(id)}
function showFile(f,tags) {
//console.log(tags);
var tr = document.createElement('tr');
var t2 = guessSong(f.webkitRelativePath || f.mozFullPath || f.fileName);
//it should be innerText/contentText but its annoying.
var td = document.createElement('td');
td.innerHTML = tags.Title || t2.Title;
tr.appendChild(td);
var td = document.createElement('td');
td.innerHTML = tags.Artist || t2.Artist;
tr.appendChild(td);
var td = document.createElement('td');
td.innerHTML = tags.Album || t2.Album;
tr.appendChild(td);
var td = document.createElement('td');
td.innerHTML = tags.Genre || "";
tr.appendChild(td);
tr.onclick = function(){
var pl = document.createElement('tr');
var st = document.createElement('td');
st.innerHTML = tags.Title || t2.Title;
pl.appendChild(st);
$("playtable").appendChild(pl);
pl.file = f;
pl.className = 'visible';
pl.onclick = function(e){
if(e && e.button == 1){
pl.parentNode.removeChild(pl);
}else{
var url;
if(window.createObjectURL){
url = window.createObjectURL(f)
}else if(window.createBlobURL){
url = window.createBlobURL(f)
}else if(window.URL && window.URL.createObjectURL){
url = window.URL.createObjectURL(f)
}else if(window.webkitURL && window.webkitURL.createObjectURL){
url = window.webkitURL.createObjectURL(f)
}
$("player").src = url;
$("player").play();
for(var i = document.querySelectorAll('.playing'), l = i.length; l--;){
i[l].className = '';
}
pl.className += ' playing';
currentSong = pl;
}
}
if($("playtable").childNodes.length == 1) pl.onclick();
}
$('songtable').appendChild(tr);
}
var numWorkers = 4;
var worker = {};
for(var i=0; i<numWorkers; i++) {
worker[i] = new Worker('worker.js');
worker[i].num = i;
worker[i].count = 0;
worker[i].status = 'working';
worker[i].onmessage = function(e) {
if(this.status=='working') {
this.count++;
//goodFiles.push(e.data.clean);
showFile(e.data.file,e.data.tags);
/*
if(queue.length>1000) {
if((this.count+(this.num*25))%100==0) { showFile(e.data.file,e.data.tags); }
} else {
showFile(e.data.file,e.data.tags);
}
*/
processNext(this);
/* Maybe they just need some rest?
if(this.count%100==0) {
// Good job son! You've earned a break!
this.status = 'resting';
console.log("Worker "+this.num+" is resting");
window.setTimeout("wakeUp("+eval(this.num)+")",10000);
} else {
processNext(this);
}
*/
}
}
}
function processNext(worker) {
if(queue.length>0) {
var next = queue.shift();
worker.postMessage(next);
if(queue.length%100==0) { console.log(queue.length); }
//console.log(queue.length);
window.setTimeout("rescueWorker("+eval(worker.num)+","+eval(worker.count)+")",300);
} else {
workEnd = new Date().getTime();
console.log('Completed in '+(workEnd-workStart)+'ms');
worker.status = 'done';
worker.terminate();
}
}
function rescueWorker(num,count) {
// Man down!
if(worker[num].status=='working' && worker[num].count==count) {
processNext(worker[num]);
}
}
function wakeUp(i) {
// Quit lying around! Back to work!
console.log('revive! '+i);
worker[i].status = 'working';
processNext(worker[i]);
}
var queue = [];
var workStart = 0;
var workEnd = 0;
function getSongs(files) {
workStart = new Date().getTime();
$("mask").style.display = 'none';
$("startup").style.display = 'none';
var mp3 = canPlay('audio/mpeg;'), ogg = canPlay('audio/ogg; codecs="vorbis"');
for(var i = 0; i < files.length; i++){
var file = files[i];
var path = file.webkitRelativePath || file.mozFullPath || file.fileName;
if (path.indexOf('.AppleDouble') != -1) {
// Meta-data folder on Apple file systems, skip
continue;
}
var size = file.size || file.fileSize || 4096;
if(size < 4095) {
// Most probably not a real MP3
//console.log(path);
continue;
}
if(file.fileName.indexOf('mp3') != -1){ //only does mp3 for now
if(mp3){
queue.push(file);
}
}
if(file.fileName.indexOf('ogg') != -1 || file.fileName.indexOf('oga') != -1){
if(ogg){
queue.push(file);
}
}
}
console.log(files);
console.log(queue.length);
for(var i=0; i<numWorkers; i++) {
processNext(worker[i]);
}
}
var currentSong = 0;
function nextSong(){
try{
currentSong.nextSibling.onclick();
}catch(e){
currentSong = document.querySelector("#playtable tr");
currentSong.onclick();
}
}
function shuffle(){
var pt = document.getElementById('playtable');
//fisher yates shuffle. hopefully.
for(var i = document.querySelectorAll("#playtable tr"), l = i.length; l--;){
var j = Math.floor(Math.random() * l);
var jel = i[j], iel = i[l];
var jref = jel.nextSibling, iref = iel.nextSibling;
pt.insertBefore(jel, iref);
pt.insertBefore(iel, jref);
}
}
function empty(){
var pt = document.getElementById('playtable');
pt.innerHTML = '';
}
onload = function(){
//with no dependencies, it should be fine to use this instead of ondomcontentloaded
var a = document.createElement('audio');
if(!a.canPlayType) $("support").innerHTML += "Your browser does not support HTML5 Audio<br>";
if(!(a.canPlayType && a.canPlayType('audio/ogg; codecs="vorbis"').replace(/no/, '')))
$("support").innerHTML += "Your browser does not support Ogg Vorbis Playback<br>";
if(!(a.canPlayType && a.canPlayType('audio/mpeg;').replace(/no/, '')))
$("support").innerHTML += "Your browser does not support MP3 Playback<br>";
var f = document.createElement('input');
f.type = 'file';
if(!('multiple' in f)) $("support").innerHTML += "Your browser does not support selecting multiple files<br>";
if(!('webkitdirectory' in f)) $("support").innerHTML += "Your browser probably does not support selecting directories<br>";
if(window.createObjectURL){}else if(window.createBlobURL){}else if(window.URL && window.URL.createObjectURL){
}else if(window.webkitURL && window.webkitURL.createObjectURL){}else{
$("support").innerHTML += "Your browser probably does not support Object URLs<br>";
}
document.querySelector('#search input').onkeydown = function(e){
if(e.keyCode == 13){
for(var i = document.querySelectorAll('#songtable tr.visible'), l = i.length; l--;){
i[l].onclick();
}
}
}
}
//doesnt quite belong here but doesnt quite belong elsewhere either
function guessSong(n){
var parts = unescape(n)
.replace(/^.*(music|desktop|document|video|home)/gi,'')
.replace(/\[[^\]]+\]/g,'')
.replace(/_/g,' ')
.replace(/[0-9]+\s*(kbps|\-)/,'')
.split(/[\/]/)
.reverse();
var name = parts[0]
.replace(/\.(mp3|ogg|flac)/,'')
.replace(/^\s|\s$/g,'')
.replace(/^\d+\s*/,'');
var artist='Unknown',album='Unknown';
if(/\-/.test(name)){
var np = name.split('-').reverse();
name = np[0];
if(np.length >= 2){
artist = np[1];
}
if(np.length >= 3){
artist = np[2];
album = np[1]
}
if(parts[1] && parts[2]){
var album = parts[2]
.replace(/^\s|\s$/g,'');
if(!album){
album = parts[1]
.replace(/^\s|\s$/g,'');
}
}
}else{
if(parts[1]){
var album = parts[1]
.replace(/^\s|\s$/g,'')
}
if(parts[2]){
var artist = parts[2]
.replace(/^\s|\s$/g,'')
.replace(/^\d+\s*/,'');
}
}
if(/\-/.test(album)){
var as = album.split('-');
album = as[1];
if(!artist){
artist = as[0];
}
}
return {
Title: name,
Artist: artist,
Album: album
}
}
</script>
</head>
<body>
<div id="mask"></div>
<div id="startup">
<div id="prompt">
<center>
<input type="file" webkitdirectory directory multiple mozdirectory onchange="getSongs(this.files)">
</center>
<p style="padding-left: 15px">
This is <b>almost certainly</b> the first mp3 player of its kind. Right above you should see a file prompt, go and select your <b>music folder</b>. It will index songs on your hard drive, read ID3 tags, play songs and it works offline. <b>HTML5 is awesome.</b>
</p>
<p style="padding-left: 15px">
By <a href="http://twitter.com/antimatter15">@antimatter15</a> / <a href="http://twitter.com/ptrwtts">@ptrwtts</a>
</p>
</div>
<div id="support" style="position:absolute;z-index:99999;color:red;font-size:x-large"> <!-- insert cheap knockoff modernizer clone -->
</div>
</div>
<div id="search">
<input type="text" placeholder="filter library" spellcheck=off autocomplete=off oninput="runSearch(this.value)">
</div>
<div style="height: 37px"></div>
<div id="playlist">
<table id="playtable"></table>
</div>
<div id="listtools">
<a href="javascript:shuffle()">Shuffle</a> /
<a href="javascript:empty()">Clear</a>
</div>
<div id="songs">
<table id="songtable" cellspacing=0 cellpadding=0>
</table>
</div>
<div style="height: 50px"></div>
<div id="footer">
<audio onended="nextSong()" controls id="player">
</div>
</body>
</html>