-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmusic.js
More file actions
556 lines (419 loc) · 17 KB
/
music.js
File metadata and controls
556 lines (419 loc) · 17 KB
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
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
window.onload = function() {
// 创建最外层包裹盒子
var videoBox = document.createElement('div');
videoBox.className = 'videoBox';
document.body.appendChild(videoBox);
// 创建video盒子
var videof = document.createElement('div');
videof.className = 'videof';
videoBox.appendChild(videof);
// 盒子转动
// videof.style = 'animation: rotate 5s linear infinite;';
// 创建video
videof.innerHTML = `<audio id="mp3" src="${musicList[0]}" ></audio>`;
// 获取video
var video = document.querySelector('#mp3');
// video.play()
// 创建暂停播放标志
var musicButton = document.createElement('div');
musicButton.className = 'music-button';
videoBox.appendChild(musicButton);
// 下方控制台
var g_btmbar = document.createElement('div');
g_btmbar.className = 'g-btmbar';
document.body.appendChild(g_btmbar);
// 内部盒子
g_btmbar.innerHTML =
`
<div class="m-playbar m-playbar-lock m-playbar-unlock" style="top: -53px; visibility: visible;" id="auto-id-iFtNdU0MHEn00ChH">
<div class="updn">
<div class="left f-fl">
<a id="lock" href="javascript:;" class="btn" hidefocus="true" data-action="lock"></a>
</div>
<div class="right f-fl"></div>
</div>
<div class="bg"></div>
<div id="barhand" class="hand" title="展开播放条"></div>
<div class="wrap" id="g_player" style="margin-left: -498.5px;">
<div class="btns">
<a href="javascript:;" hidefocus="true" data-action="prev" class="prv" title="上一首(ctrl+←)">上一首</a>
<a href="javascript:;" hidefocus="true" data-action="play" class="ply j-flag" title="播放/暂停(p)">播放/暂停</a>
<a href="javascript:;" hidefocus="true" data-action="next" class="nxt" title="下一首(ctrl+→)">下一首</a>
</div>
<div class="head j-flag"><img id="musicImg" src="./images/bg1.jpg">
<a href="javascript:;" hidefocus="true" class="mask"></a>
</div>
<div class="play">
<div class="j-flag words">
<a id="musicName" hidefocus="true" href="javascript:;" class="f-thide name fc1 f-fl" title="Justin Bieber-As Long As You Love Me(HEST remix)">Justin Bieber-As Long As You Love Me(HEST remix)</a>
</div>
<div class="m-pbar" data-action="noop">
<div class="barbg j-flag" id="auto-id-FMI4WdprNUwUHTUz">
<div class="rdy" style="width: 0%;"></div>
<div class="cur" style="width:0%;"><span class="btn f-tdn f-alpha" id="auto-id-vyfkHi306n02dohr"><i></i></span></div>
</div>
<span id="musicTime" class="j-flag time"><em>00:00</em> / 00:00</span>
</div>
</div>
<div class="ctrl f-fl f-pr j-flag">
<div class="m-vol" style="visibility:hidden;" id="auto-id-RXTm1mf0REcUKUyC">
<div class="barbg"></div>
<div class="vbg j-t" id="auto-id-6PNSIg0ZORWvXyrf">
<div class="curr j-t" style="height: 11.4815px;"></div>
<span id="volLodging" class="btn f-alpha j-t" style="top: 71px;"></span></div>
</div>
<a id="musicVol" href="javascript:;" hidefocus="true" data-action="volume" class="icn icn-vol"></a>
<a id="loop" href="javascript:;" hidefocus="true" data-action="mode" class="icn icn-loop" title="循环"></a>
<div class="tip tip-1" style="display: none;">循环</div>
</div>
</div>
</div>
`;
// 获取一些元素
var prv = document.querySelector('.prv');
var ply = document.querySelector('.ply');
var nxt = document.querySelector('.nxt');
var loading = document.querySelector('#auto-id-vyfkHi306n02dohr i');
var loadingSpan = document.querySelector('#auto-id-vyfkHi306n02dohr');
var musicName = document.querySelector('#musicName');
var musicImg = document.querySelector('#musicImg');
var bar = document.querySelector('#auto-id-FMI4WdprNUwUHTUz');
var rdy = document.querySelector('.rdy');
var cur = document.querySelector('.cur');
var musicTime = document.querySelector('#musicTime');
var isLoop = document.querySelector('#loop');
var loopmes = document.querySelector('.tip.tip-1');
var musicvol = document.querySelector('#musicVol');
var musicvoldiv = document.querySelector('#auto-id-RXTm1mf0REcUKUyC');
var musicvolBox = document.querySelector('#auto-id-6PNSIg0ZORWvXyrf');
var volLodging = document.querySelector('#volLodging');
var curr = document.querySelector('.curr');
var lock = document.querySelector('#lock');
var playBar = document.querySelector('#auto-id-iFtNdU0MHEn00ChH');
var barhand = document.querySelector('#barhand');
// 开始暂停图标点击事件
musicButton.addEventListener('click', function() {
// 有stop类则执行if
if (videoBox.classList.toggle('stop')) {
// 当前应该启动
ply.classList.toggle('pas');
video.play();
videof.style.animation = 'rotate 5s linear infinite';
} else {
// 当前应该暂停播放
ply.classList.toggle('pas');
video.pause();
videof.style.animation = '';
}
})
// 点击下方控制台开始暂停
ply.addEventListener('click', function() {
musicButton.click();
})
// 音乐载入事件
video.addEventListener('canplay', function() {
var nameF = musicList[nowMusic].split('/');
var name = nameF[nameF.length - 1];
name = name.split('.')[0];
musicName.innerHTML = name;
var imgF = window.getComputedStyle(videof).backgroundImage.split('"');
var img = imgF[1];
musicImg.src = img;
musicTime.innerHTML = "<em>00:00</em> / " + sToM(video.duration) + "</span>";
curr.style.height = 100 + '%';
volLodging.style.top = -7 + 'px';
loading.style.visibility = 'hidden';
})
// 音乐缓冲暂停事件
video.addEventListener('waiting', function() {
loading.style.visibility = 'visible';
})
// 音乐正在播放事件
video.addEventListener('timeupdate', musicPlay)
function musicPlay() {
musicTime.innerHTML = "<em>" + sToM(video.currentTime) + "</em> / " + sToM(video.duration) + "</span>";
cur.style.width = Percentage(video.currentTime, video.duration) + '%';
try {
rdy.style.width = Percentage(video.buffered.end(video.buffered.length - 1), video.duration) + '%';
} catch (err) {
console.log(err);
}
}
// 音乐播放事件
video.addEventListener('playing', function() {
loading.style.visibility = 'hidden';
})
// 下一首
nxt.addEventListener('click', function() {
nowMusic = (nowMusic + 1) % musicList.length;
replaceCover(musicImgList[nowMusic]);
replaceMusic(musicList[nowMusic]);
})
// 上一首
prv.addEventListener('click', function() {
nowMusic = (nowMusic - 1 < 0 ? musicList.length - 1 : nowMusic - 1) % musicList.length;
replaceCover(musicImgList[nowMusic]);
replaceMusic(musicList[nowMusic]);
})
// 循环播放
isLoop.addEventListener('click', function() {
isLoop.classList.toggle('icn-one');
if (isLoop.classList.toggle('icn-loop')) {
// 单曲循环
video.loop = false;
} else {
video.loop = true;
}
loopmes.style.display = 'block';
video.loop ? loopmes.innerHTML = '单曲循环' : loopmes.innerHTML = '循环';
setTimeout(function() {
loopmes.style.display = 'none';
}, 1500)
})
video.addEventListener('ended', function() {
nxt.click();
})
video.addEventListener('emptied', () => {
loading.style.visibility = 'visible';
})
video.addEventListener('error', () => {
loading.style.visibility = 'visible';
})
// 进度条事件
// 点击进度条
bar.addEventListener('mousedown', function(e) {
var time = Percentage(e.pageX - bar.getBoundingClientRect().x, bar.getBoundingClientRect().width);
cur.style.width = time + '%';
try {
video.currentTime = outPercentage(time, video.duration);
} catch (err) {
console.error(err);
}
if (video.networkState == 3) {
cur.style.width = 0 + '%';
}
})
loadingSpan.onmousedown = function(e) {
var time;
document.onmousemove = function(e) {
video.removeEventListener('timeupdate', musicPlay);
if (e.pageX != 0) {
time = Percentage(e.pageX - bar.getBoundingClientRect().x, bar.getBoundingClientRect().width);
if (time <= 100) {
musicTime.innerHTML = "<em>" + sToM(outPercentage(time, video.duration)) + "</em> / " + sToM(video.duration) + "</span>";
cur.style.width = time + '%';
} else {
musicTime.innerHTML = "<em>" + sToM(outPercentage(100, video.duration)) + "</em> / " + sToM(video.duration) + "</span>";
cur.style.width = 100 + '%';
}
// video.currentTime = outPercentage(time, video.duration);
}
}
document.onmouseup = function(e) {
video.addEventListener('timeupdate', musicPlay);
try {
video.currentTime = outPercentage(time, video.duration);
} catch (err) {
console.error(err);
}
document.onmousemove = null;
document.onmouseup = null;
if (video.networkState == 3)
cur.style.width = 0 + '%';
}
}
// 阻止禁止标志出现
var mPbar = document.querySelector('.m-pbar');
mPbar.addEventListener('dragenter', function(e) {
e.preventDefault();
document.onmousemove = null;
document.onmouseup = null;
})
mPbar.addEventListener('dragover', function(e) {
e.preventDefault();
console.log(e.dataTransfer);
document.onmousemove = null;
document.onmouseup = null;
e.dataTransfer.dropEffect = "none";
console.log(e.dataTransfer);
})
// 声音
musicvol.addEventListener('click', function(e) {
e.stopPropagation();
if (musicvol.classList.toggle('hid')) {
musicvoldiv.style.visibility = 'initial';
} else {
musicvoldiv.style.visibility = 'hidden';
}
})
musicvolBox.addEventListener('mousedown', function(e) {
var vol = Percentage(musicvolBox.getBoundingClientRect().height + musicvolBox.getBoundingClientRect().y - e.pageY, musicvolBox.getBoundingClientRect().height);
if (vol <= 0) {
curr.style.height = 0 + '%';
volLodging.style.top = musicvolBox.getBoundingClientRect().height - 7 + 'px';
video.volume = 0;
musicvol.classList.add('icn-volno');
} else if (vol <= 100) {
curr.style.height = vol + '%';
volLodging.style.top = e.pageY - musicvolBox.getBoundingClientRect().y - 7 + 'px';
video.volume = vol / 100;
musicvol.classList.remove('icn-volno');
} else {
curr.style.height = 100 + '%';
volLodging.style.top = -7 + 'px';
video.volume = 1;
}
})
document.addEventListener('click', function(e) {
musicvol.classList.remove('hid');
musicvoldiv.style.visibility = 'hidden';
if (playBar.classList.length == 3) {
down = setTimeout(function() {
playBar.style.top = -7 + 'px';
}, 1000)
}
})
musicvoldiv.addEventListener('click', function(e) {
e.stopPropagation();
})
volLodging.onmousedown = function(e) {
document.onmousemove = function(e) {
video.removeEventListener('timeupdate', musicPlay);
var vol = Percentage(musicvolBox.getBoundingClientRect().height + musicvolBox.getBoundingClientRect().y - e.pageY, musicvolBox.getBoundingClientRect().height);
if (vol <= 0) {
curr.style.height = 0 + '%';
volLodging.style.top = musicvolBox.getBoundingClientRect().height - 7 + 'px';
video.volume = 0;
musicvol.classList.add('icn-volno');
} else if (vol <= 100) {
curr.style.height = vol + '%';
volLodging.style.top = e.pageY - musicvolBox.getBoundingClientRect().y - 7 + 'px';
video.volume = vol / 100;
musicvol.classList.remove('icn-volno');
} else {
curr.style.height = 100 + '%';
volLodging.style.top = -7 + 'px';
video.volume = 1;
}
// video.currentTime = outPercentage(time, video.duration);
}
document.onmouseup = function(e) {
document.onmousemove = null;
document.onmouseup = null;
}
}
// 定时器下降
var down = setTimeout(function() {
playBar.style.top = -7 + 'px';
}, 1000)
var lockdown;
// 锁标志切换
lock.addEventListener('click', function(e) {
if (playBar.classList.toggle('m-playbar-unlock')) {
down = setTimeout(function() {
playBar.style.top = -7 + 'px';
}, 1000)
} else {
clearTimeout(down);
clearTimeout(lockdown);
playBar.style.top = -53 + 'px';
}
})
playBar.addEventListener('mouseover', function() {
clearTimeout(down);
})
barhand.addEventListener('mouseover', function() {
clearTimeout(down);
playBar.style.top = -53 + 'px';
})
barhand.addEventListener('mouseout', function() {
if (playBar.classList.length == 3) {
down = setTimeout(function() {
playBar.style.top = -7 + 'px';
}, 1000)
}
})
lock.addEventListener('mouseout', function(e) {
e.stopPropagation();
if (playBar.classList.length == 3) {
lockdown = setTimeout(function() {
playBar.style.top = -7 + 'px';
}, 1000)
}
}, true)
}
var musicList = ['./Localmusic/大鱼.mp3', './Localmusic/兮曰Crandy,叶栖夏 - 【词策】魔道祖师同人曲云梦纯歌版(翻自 高杉さと美).mp3', './Localmusic/兮曰Crandy - 东风志.mp3'];
var musicImgList = ['./images/bg1.jpg', './images/bg2.jpg', './images/bg3.jpg'];
var nowMusic = 0;
//添加多个歌曲
function pushMusicList(objArr) {
if (objArr.length != 0) {
for (const index in objArr) {
musicList.push(objArr[index].musicName);
musicImgList.push(objArr[index].musicCover || './images/bg1.jpg');
}
}
}
//添加单个歌曲
function pushMusic(obj) {
if (obj) {
musicList.push(obj["musicName"]);
musicImgList.push(obj["musicCover"] || './images/bg1.jpg');
}
}
//删除原有所有歌曲
function clearMusicList() {
musicList = [];
musicImgList = [];
}
// 更换歌曲封面
function replaceCover(path) {
var videof = document.querySelector('.videof');
path = path == undefined ? './images/bg1.jpg' : path;
videof.style.backgroundImage = 'url(' + path + ')';
}
// 更换歌曲
function replaceMusic(path) {
var video = document.querySelector('#mp3');
var videoBox = document.querySelector('.videoBox');
video.src = path;
// video.play();
if (videoBox.className.indexOf('stop') != -1) {
video.play();
} else {
video.pause();
}
}
function sToM(s) {
//计算分钟
//算法:将秒数除以60,然后下舍入,既得到分钟数
var h;
h = Math.floor(s / 60);
//计算秒
//算法:取得秒%60的余数,既得到秒数
s = Math.floor(s % 60);
//将变量转换为字符串
h += '';
s += '';
//如果只有一位数,前面增加一个0
h = (h.length == 1) ? '0' + h : h;
s = (s.length == 1) ? '0' + s : s;
if (isNaN(h)) {
h = "00";
s = "00";
}
return h + ':' + s;
}
function Percentage(num, total) {
if (num == 0 || total == 0) {
return 0;
}
return (Math.round(num / total * 10000) / 100.00); // 小数点后两位百分比
}
function outPercentage(num, total) {
if (num == 0 || total == 0) {
return 0;
}
return (Math.round(total * num / 100)); // 小数点后两位百分比
}
console.log("%cGitHub地址:https://github.com/queuecat/Music-Playing", "color:#0aa");