forked from gb1998/MusicPlay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMusicPlay.html
737 lines (679 loc) · 18 KB
/
MusicPlay.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
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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>MusicPlay</title>
<script>
window.onload = function () {
var audio = document.getElementById('audio'), // 音乐
sourceList = audio.getElementsByTagName('source'), // 音乐列表
play = document.getElementById('play'), // 播放按钮
prev = document.getElementById('prev'), // 上一曲
next = document.getElementById('next'), // 下一曲
jingyin = document.getElementById('jingyin'), // 静音
voiceBar = document.getElementById('voice-bar'), // 音量调节
musicBar = document.getElementById('music-bar'), // 播放进度控制
playedBar = document.getElementById('played-bar'), // 已播放进度条
voicedBar = document.getElementById('voiced-bar'), // 已播放进度条
musicTitle = document.getElementById('title'), // 音乐标题
loadBar = document.getElementById('load-bar'), // 加载进度条
currentTime = document.getElementById('current-time'), // 当前音乐时间
totalTime = document.getElementById('total-time'), // 当前音乐时间
musicImg = document.getElementById('music-img'), // 音乐封面
xunhuan = document.getElementById('xunhuan'); // 循环按钮
currentSrcIndex = 0;
// 网易云音乐模块
var keyword = document.getElementById('keyword'); // 搜索歌名
var searchBtn = document.getElementById('search-btn'); // 搜索按钮
var result = document.getElementById('result'); // 结果区
var toPlay = document.getElementById('to-play'); // 立即播放按钮
// 是否循环播放
audio.loop = false;
// 是否自动播放
audio.autoplay = false;
// 初始化音量
audio.volume = 0.5;
var voicedBarWidth = (audio.volume / 1) * voiceBar.clientWidth;
voicedBar.style.width = voicedBarWidth + 'px';
// 是否自动缓冲加载
audio.autobuffer = false;
// 显示第一首歌曲时长
(function () {
var fen = parseInt(audio.duration / 60);
var miao = parseInt(audio.duration % 60);
if (miao < 10) {
miao = '0'+miao;
}
totalTime.innerHTML = fen + ':' + miao;
})();
// 播放 暂停
play.onclick = function () {
if (audio.paused) {
audio.play();
this.innerHTML = 'Pause';
musicImg.style.animation = 'xuanzhuan 5s linear infinite';
} else {
audio.pause();
this.innerHTML = 'Play';
musicImg.removeAttribute('style');
}
musicTime();
};
// 下一曲
next.onclick = function () {
changeMusic('next');
};
// 上一曲
prev.onclick = function () {
changeMusic('prev');
};
// 切换歌曲
function changeMusic (direct) {
if (direct === 'next') {
++ currentSrcIndex > sourceList.length - 1 && (currentSrcIndex = 0); // 下一曲
} else {
-- currentSrcIndex < 0 && (currentSrcIndex = sourceList.length -1); // 上一曲
}
currentSrc = sourceList[currentSrcIndex].getAttribute('src');
currentImg = sourceList[currentSrcIndex].getAttribute('data-img')
musicImg.setAttribute('src', currentImg);
audio.setAttribute('src', currentSrc);
audio.play();
play.innerHTML = 'Pause';
musicImg.style.animation = 'xuanzhuan 5s linear infinite';
musicTime();
}
voiceBar.onclick = function (event) {
var voiceBarWidth = voiceBar.clientWidth;
var newVolume = (event.offsetX / voiceBarWidth);
audio.volume = newVolume;
var voicedBarWidth = (audio.volume / 1) * voiceBarWidth;
voicedBar.style.width = voicedBarWidth + 'px';
};
// 播放进度控制
musicBar.onclick = function (event) {
var musicBarWidth = musicBar.clientWidth;
var newCurrentTime = (event.offsetX / musicBarWidth) * audio.duration;
audio.currentTime = newCurrentTime;
var playedBarWidth = (audio.currentTime / audio.duration) * musicBarWidth;
playedBar.style.width = playedBarWidth + 'px';
};
// 播放进度实时更新(修改为歌曲播放时开启定时器,暂停和页面load时清除定时器)
setInterval(function updatePlayedBar (){
var musicBarWidth = musicBar.clientWidth;
var playedBarWidth = (audio.currentTime / audio.duration) * musicBarWidth;
playedBar.style.width = playedBarWidth + 'px';
if (audio.currentTime % 60 < 10) {
currentTime.innerHTML = parseInt(audio.currentTime / 60) + ':0' + parseInt(audio.currentTime % 60);
} else {
currentTime.innerHTML = parseInt(audio.currentTime / 60) + ':' + parseInt(audio.currentTime % 60);
}
//如果是时间结束,并且是非单曲循环,自动下一曲
if (audio.currentTime === audio.duration && !audio.loop) {
next.onclick();
}
}, 1000);
// 静音
jingyin.onclick = function () {
if (!audio.muted) {
audio.muted = true;
voicedBar.style.width = 0 +'px';
}else {
audio.muted = false;
var voiceBarWidth = voiceBar.clientWidth;
// 音量大小更新
var voicedBarWidth = (audio.volume / 1) * voiceBarWidth;
voicedBar.style.width = voicedBarWidth + 'px';
}
};
// 判断文件缓冲进度
setInterval(function updateCache () {
var buffered, percent;
// 已缓冲部分
audio.readyState == 4 && (buffered = audio.buffered.end(0));
// 已缓冲百分百
audio.readyState == 4 && (percent = Math.round(buffered / audio.duration * 100) + '%');
loadBar.style.width = (Math.round(buffered / audio.duration * 100) * musicBar.clientWidth * 0.01) + 'px';
}, 1000);
// 计算音乐时间
function musicTime() {
// 更换播放歌曲
musicTitle.innerHTML = sourceList[currentSrcIndex].title;
// 播放时间显示
audio.addEventListener("canplay", function(){
var sc=audio.duration;
var fen = parseInt(sc / 60);
var miao = parseInt(sc % 60);
if (miao < 10) {
totalTime.innerHTML = fen + ':0' + miao;
}else {
totalTime.innerHTML = fen + ':' + miao;
}
});
};
// 是否单曲循环
xunhuan.onclick = function () {
if (audio.loop) {
audio.loop = false;
this.innerHTML = '循环';
} else {
audio.loop = true;
this.innerHTML = '单曲';
}
};
var toPlay = document.getElementById('to-play'); // 立即播放按钮
var result = document.getElementById('result'); // 结果区
// 利用委托来为立即播放绑定事件
result.addEventListener('click', function (ev) {
var t = ev.target || ev.srcElement;
if (t.tagName === 'A') {
var oMusicSrc = result.getAttribute('data-audio');
var oMusicImg = result.getAttribute('data-img');
var oMusicName = result.getAttribute('data-music');
var oSinger = result.getAttribute('data-singer');
musicImg.setAttribute('src',oMusicImg);
musicTitle.innerHTML = oMusicName + '-' + oSinger;
audio.setAttribute('src', oMusicSrc);
audio.play();
play.innerHTML = 'Pause';
musicImg.style.animation = 'xuanzhuan 5s linear infinite';
}
});
// 搜索按钮
searchBtn.addEventListener('click', function () {
var value = keyword.value;
if (!value) {
alert('关键词不能为空');
return;
}
//http://music.163.com/api/song/lyric?os=pc&id=" . $music_id . "&lv=-1&kv=-1&tv=-1.歌词的链接
var url = "http://s.music.163.com/search/get/";
var data = {
"type": 1,//单曲
"limit": 1,//返回数量1
"s": value,//搜索词
"callback": "jsonpcallback"
};
var buffer = [];
for (var key in data) {
buffer.push(key + '=' + encodeURIComponent(data[key]));
}
var fullpath = url + '?' + buffer.join('&');
CreateScript(fullpath);
});
//异步加载js不影响当前渲染结果
function CreateScript (src) {
var el = document.createElement('script');
el.src = src;//加载url
el.async = true;
el.defer = true;
document.body.appendChild(el);
};
};
</script>
<style>
* {
margin: 0;
padding: 0;
}
html,body {
height: 100%;
font: lighter 16px -apple-system,PingFang SC,Hiragino Sans GB,Arial,Microsoft YaHei,Helvetica Neue,sans-serif;
margin: 0;
padding: 0;
}
a {
text-decoration: none;
color: #fff;
}
.container {
overflow: hidden;
width: 1020px;
margin: 0 auto;
padding-top: 20px;
}
.music {
width: 320px;
height: 470px;
float: left;
background: rgba(0,0,0,0.3);
color: #fff;
border-radius: 3px;
}
.header {
height: 60px;
text-align: center;
line-height: 60px;
border-bottom: 1px solid #fec514;
}
.header h1 {
font-size: 24px;
color: #a2a2a2;
}
.fengmian {
height: 260px;
position: relative;
}
.fengmian img {
position: absolute;
left: 50%;
top: 50%;
margin-left: -64px;
margin-top: -64px;
width: 128px;
height: 128px;
border-radius: 50%;
}
#title {
text-align: center;
height: 40px;
line-height: 40px;
}
.jindu {
padding-top: 5px;
height: 25px;
}
#music-bar {
display: inline-block;
width: 218px;
height: 4px;
background: #6d6d6d;
margin-right: 7px;
border-radius: 4px;
margin-left: 5px;
position: relative;
}
#voice-bar {
display: inline-block;
width: 80px;
height: 4px;
background: #6d6d6d;
border-radius: 4px;
position: relative;
}
#played-bar, #voiced-bar {
display: inline-block;
height: 4px;
float: left;
border-radius: 4px;
background: -webkit-linear-gradient(left, #fec514, #EE4000);
background: linear-gradient(left, #fec514, #EE4000);
position: absolute;
z-index: 2;
transition: all 0.5s;
}
#played-bar:after, #voiced-bar:after {
content: '';
width: 8px;
height: 8px;
border-radius: 50%;
background: #fff;
position: absolute;
right: -2px;
top: -2px;
}
#load-bar {
display: inline-block;
height: 4px;
float: left;
border-radius: 4px;
background: #a2a2a2;
position: absolute;
z-index: 1;
transition: all 3s; /* 进度加载动画 */
}
#time {
width: 216px;
font-size: 12px;
overflow: hidden;
padding: 3px 0 0 6px;
}
#time span:nth-child(1) {
float: left;
}
#time span:nth-child(2) {
float: right;
}
.controls {
padding-top: 20px;
text-align: center;
}
.controls a {
display: inline-block;
color:#000;
width: 50px;
height: 50px;
line-height: 50px;
text-align: center;
margin-right: 5px;
border-radius: 50%;
background: #fec514;
}
#play {
font-size: 14px;
transition: all 0.3s;
}
#play:hover {
background: rgba(254,197,20,0.7);
}
#prev, #next {
width: 30px;
height: 30px;
line-height: 30px;
background: transparent;
color: #fec514;
border: 2px solid #fec514;
transition: all 0.3s;
}
#prev:hover, #next:hover {
border-color: rgba(254,197,20,0.7);
}
#xunhuan, #jingyin {
width: 30px;
height: 16px;
line-height: 16px;
font-size: 12px;
background: transparent;
color: #fec514;
margin: 0 20px;
}
/* 歌单部分 */
.music-list {
width: 680px;
height: 470px;
margin-right: 20px;
float: left;
color: #ccc;
}
.music-list .title {
height: 40px;
line-height: 40px;
text-align: center;
font-size: 22px;
}
.music-list .list {
padding: 20px;
}
.music-list .list li {
display: block;
line-height: 30px;
border-bottom: 1px dotted #ccc;
}
/* 搜索部分 */
.search {
height: 100px;
margin: 0 auto;
width: 1020px;
color: #ccc;
position: relative;
}
#keyword {
width: 219px;
height: 35px;
line-height: 35px;
border: 1px solid #ccc;
color: #ccc;
padding: 0 10px;
border-radius: 20px;
background: transparent;
outline: none;
font-size: 14px;
position: absolute;
top: 30px;
left: 10px;
}
#search-btn {
display: inline-block;
height: 35px;
line-height: 35px;
border: 1px solid #ccc;
font-size: 14px;
padding: 0 10px;
border-radius: 0 20px 20px 0;
position: absolute;
top: 30px;
left: 186px;
}
#result {
display: inline-block;
border: 1px dashed #ccc;
height: 35px;
line-height: 35px;
padding: 0 20px;
border-radius: 20px;
font-size: 14px;
opacity: 0;
transition: all 0.7s;
position: absolute;
top: 30px;
left: 320px;
}
#result strong {
font-weight: normal;
margin-right: 20px;
}
#to-play {
color: #fec514;
}
/* 渐变质感背景 */
.blueHour {
background: -webkit-linear-gradient(left, #7E898F 0%, #636C75 100%), -webkit-radial-gradient(at top, rgba(255, 255, 255, 0.5) 0%, rgba(0, 0, 0, 0.55) 100%), -webkit-radial-gradient(at top, rgba(255, 255, 255, 0.5) 0%, rgba(0, 0, 0, 0.08) 63%);
background: linear-gradient(90deg, #7E898F 0%, #636C75 100%), radial-gradient(at top, rgba(255, 255, 255, 0.5) 0%, rgba(0, 0, 0, 0.55) 100%), radial-gradient(at top, rgba(255, 255, 255, 0.5) 0%, rgba(0, 0, 0, 0.08) 63%);
background-blend-mode: multiply, screen; }
@keyframes xuanzhuan {
0% {
transform: rotate(0deg);
}
50% {
transform: rotate(180deg);
}
100% {
transform: rotate(360deg);
}
}
/* 响应式 */
@media screen and (max-width: 450px){
body {
background: #fff;
}
.container {
width: 100%;
height: 100%;
padding: 0;
}
.music-list, #footer {
display: none;
}
.music {
width: 100%;
height: 100%;
}
.controls {
position: absolute;
bottom: 10px;
left: 0;
width: 100%;
text-align: center;
}
#music-bar {
width: 70%;
margin-left: 5%;
margin-right: 5%;
}
#voice-bar {
width: 15%;
}
#time {
width: 70%;
margin-left: 5%;
margin-right: 5%;
padding-left: 0;
}
.jindu {
position: absolute;
bottom: 80px;
left: 0;
width: 100%;
}
#title {
position: absolute;
bottom: 110px;
left: 0;
width: 100%;
}
.search {
width: 100%;
position: absolute;
bottom: 170px;
}
#result {
padding: 0 5%;
width: 80%;
left: auto;
right: 5%;
top: 75px;
transition: all 0.2s;
}
#keyword {
width: 80%;
padding: 0 5%;
left: auto;
right: 5%;
}
#search-btn {
left: auto;
right: 5%;
}
.fengmian img {
left: 25%;
top: 50%;
width: 50%;
height: auto;
margin: 0;
}
}
/* 无关紧要 */
#footer {
position: fixed;
right: 5px;
top: 10px;
}
#footer a {
display: inline-block;
padding: 2px 10px;
border-radius: 17px;
color: #afafaf;
border: 1px solid #afafaf;
transition: all .3s;
}
#footer a:hover {
background: #afafaf;
color: #000;
}
</style>
</head>
<body class="blueHour">
<div class="search">
<input id="keyword" type="text">
<a id="search-btn" href="javascript:;">搜一下</a>
<span id="result"></span>
</div>
<div class="container">
<div class="music-list">
<div class="title">默认歌单</div>
<div class="list">
<ul>
<a href="#"><li>你在终点等我</li></a>
<li>白昼之夜(纯音乐)</li>
<li>告白气球</li>
<li>那就这样吧</li>
<li>骄傲的少年</li>
<li>岁月神偷</li>
</ul>
</div>
</div>
<audio id="audio">
<source title="你在终点等我" data-img="http://p1.music.126.net/ddhcDeGSl9VhXJLfOsNDEA==/3433774824740403.jpg" src="http://link.hhtjim.com/163/431357712.mp3">
<source title="白昼之夜(纯音乐)" data-img="http://p1.music.126.net/he7P6CYpQmz8KqYSdULuOQ==/94557999994394.jpg" src="http://oc1475jft.bkt.clouddn.com/baizhouzhiye.mp3">
<source title="告白气球" data-img="http://p1.music.126.net/tlp3VWVQVe0Je1r-oHn91g==/17666952835430891.jpg" src="http://oc1475jft.bkt.clouddn.com/gaobaiqiqiu.mp3">
<source title="那就这样吧" data-img="http://p1.music.126.net/fYNa5MMN0KTIYP2KMpYObQ==/17923139044585525.jpg" src="http://link.hhtjim.com/163/502432901.mp3">
<source title="骄傲的少年" data-img="http://p1.music.126.net/Brn39jwEDNPVV6pNWcv_rA==/1391981724588577.jpg" src="http://link.hhtjim.com/163/408332757.mp3">
<source title="岁月神偷" data-img="http://p1.music.126.net/54wdQi_3rpjreY2oo2jb7w==/5998935441219557.jpg" src="http://link.hhtjim.com/163/28285910.mp3">
</audio>
<div class="music">
<div class="header">
<h1>MusicPlay<small>音乐播放器</small></h1>
</div>
<div class="fengmian">
<img src="img/music.png" id="music-img" alt="">
</div>
<div id="title">
hello !!!!
</div>
<div class="jindu">
<span id="music-bar">
<span id="load-bar"></span>
<span id="played-bar"></span>
</span>
<span id="voice-bar">
<span id="voiced-bar"></span>
</span>
<div id="time">
<span id="current-time">0:00</span>
<span id="total-time"></span>
</div>
</div>
<div class="controls">
<a id="xunhuan" href="javascript:;">循环</a>
<a id="prev" href="javascript:;"><</a>
<a id="play" href="javascript:;">Play</a>
<a id="next" href="javascript:;">></a>
<a id="jingyin" href="javascript:;">静音</a>
</div>
</div>
</div>
<div id="footer">
<a href="login.html" target="_blank">
<script>
var username = document.getElementById("user"); //得到账户的对象
</script>
退出登录</a>
</div>
<script>
// 回调函数将返回的内容添加到结果区(需要在页面加载完后加载上,为后面执行回调)
function jsonpcallback (rs) {
var resultHtml = '歌曲:<strong>' + rs.result.songs[0].name + '</strong>' +
'歌手:<strong>' + rs.result.songs[0].artists[0].name + '</strong>' +
'<a href="javascript:;" id="to-play">立即播放</a>';
//这个时候开始查询歌词
// http://music.163.com/api/song/lyric?os=pc&id=" . $music_id . "&lv=-1&kv=-1&tv=-1
var lyc='http://music.163.com/api/song/lyric?os=pc&id='+rs.result.songs[0].id+'&lv=-1&kv=-1&tv=-1&callback=lycjson';
alert("已经搜索到了歌词");
window.open(lyc,"新窗口歌词","width=500,height=500,toolbar=no,scrollbars=no,menubar=no,screenX=100,screenY=100");
//var el = document.createElement('script');
//el.src = 'http://music.163.com/api/song/lyric?os=pc&id='+rs.result.songs[0].id+'&lv=-1&kv=-1&tv=-1&callback=lycjson';//加载url
//el.async = true;
//el.defer = true;
//document.body.appendChild(el);
//window.open(lyc,"新窗口歌词","width=100,height=200,toolbar=no,scrollbars=no,menubar=no,screenX=100,screenY=100");
// window.location.href=lyc;
//解释json
//alert(lycjson.lrc.lyric);
// http://s.music.163.com/search/get/?type=1&limit=1&s=%E5%8C%97%E4%BA%AC%E6%AC%A2%E8%BF%8E%E4%BD%A0&callback=jsonpcallback:formatted
//http://s.music.163.com/search/get/?type=1&limit=1&s=%E6%88%91%E5%92%8C%E4%BD%A0&callback=jsonpcallback
result.innerHTML = resultHtml;
result.setAttribute('data-audio', rs.result.songs[0].audio);
result.setAttribute('data-img', rs.result.songs[0].album.picUrl);
result.setAttribute('data-music', rs.result.songs[0].name);
result.setAttribute('data-singer', rs.result.songs[0].artists[0].name);
result.style.opacity = '1';
};
function lycjson(rs){
alert('123');
var string =rs.lrc.lyric;
alert(string);
};
</script>
</body>
</html>