Spaces:
Running
Running
admin
commited on
Commit
·
5daada1
1
Parent(s):
587fbc9
2 en
Browse files- index.html +1 -1
- js/player.js +14 -31
index.html
CHANGED
@@ -23,7 +23,7 @@
|
|
23 |
<input type="file" id="musicFile" accept=".mp3,.wav,.m4a,.flac">
|
24 |
</td>
|
25 |
<td>
|
26 |
-
<input type="button" name="startStop" value="
|
27 |
</td>
|
28 |
<td>
|
29 |
<p>Upload background image👉</p>
|
|
|
23 |
<input type="file" id="musicFile" accept=".mp3,.wav,.m4a,.flac">
|
24 |
</td>
|
25 |
<td>
|
26 |
+
<input type="button" name="startStop" value="Pause" id="startStop">
|
27 |
</td>
|
28 |
<td>
|
29 |
<p>Upload background image👉</p>
|
js/player.js
CHANGED
@@ -19,21 +19,16 @@ window.onload = function () {
|
|
19 |
wrap.height = window.innerHeight - 1;
|
20 |
let canvasCtx = wrap.getContext("2d");
|
21 |
let AudioContext = window.AudioContext || window.webkitAudioContext || window.mozAudioContext;
|
22 |
-
let audioContext = new AudioContext();
|
23 |
-
|
24 |
-
// 加白色阴影
|
25 |
canvasCtx.shadowColor = "white";
|
26 |
canvasCtx.shadowBlur = 10;
|
27 |
-
|
28 |
$('#bgFile').change(function () {
|
29 |
if (this.files.length <= 0) return;
|
30 |
-
|
31 |
if (this.files[0].size > 5242880) {
|
32 |
console.log('File size larger than 5M');
|
33 |
alert('Too large!');
|
34 |
return;
|
35 |
}
|
36 |
-
|
37 |
let file = this.files[0];
|
38 |
let reader = new FileReader();
|
39 |
reader.readAsDataURL(file);
|
@@ -43,68 +38,59 @@ window.onload = function () {
|
|
43 |
});
|
44 |
|
45 |
$('#musicFile').change(function () {
|
46 |
-
//当选择歌曲时,判断当前audioContext的状态,如果在进行中则关闭音频环境,
|
47 |
-
//释放audioContext的所有资源,并重新实例化audioContext
|
48 |
if (audioContext.state == 'running') {
|
49 |
audioContext.close();
|
50 |
audioContext = new AudioContext();
|
51 |
}
|
52 |
-
|
53 |
if (this.files.length == 0) return;
|
54 |
let file = $('#musicFile')[0].files[0];
|
55 |
let fileReader = new FileReader();
|
56 |
fileReader.readAsArrayBuffer(file);
|
57 |
fileReader.onload = function (e) {
|
58 |
let count = 0;
|
59 |
-
$('#tip').text('
|
60 |
let timer = setInterval(function () {
|
61 |
count++;
|
62 |
-
$('#tip').text('
|
63 |
}, 1000)
|
64 |
audioContext.decodeAudioData(e.target.result, function (buffer) {
|
65 |
clearInterval(timer)
|
66 |
-
$('#tip').text('
|
67 |
let audioBufferSourceNode = audioContext.createBufferSource();
|
68 |
let analyser = audioContext.createAnalyser();
|
69 |
audioBufferSourceNode.connect(analyser);
|
70 |
analyser.connect(audioContext.destination);
|
71 |
audioBufferSourceNode.buffer = buffer;
|
72 |
audioBufferSourceNode.start();
|
73 |
-
|
74 |
-
//播放暂停音频
|
75 |
startStop.onclick = function () {
|
76 |
if (audioContext.state === 'running') {
|
77 |
audioContext.suspend().then(function () {
|
78 |
-
$("#startStop").val('
|
79 |
});
|
80 |
} else if (audioContext.state === 'suspended') {
|
81 |
audioContext.resume().then(function () {
|
82 |
-
$("#startStop").val('
|
83 |
});
|
84 |
}
|
85 |
}
|
86 |
let oW = wrap.width;
|
87 |
let oH = wrap.height;
|
88 |
-
|
89 |
-
//设置线条的颜色
|
90 |
let color1 = canvasCtx.createLinearGradient(oW / 2, oH / 2 - 10, oW / 2, oH / 2 - 150);
|
91 |
color1.addColorStop(0, '#1E90FF');
|
92 |
color1.addColorStop(.25, '#FF7F50');
|
93 |
color1.addColorStop(.5, '#8A2BE2');
|
94 |
color1.addColorStop(.75, '#4169E1');
|
95 |
color1.addColorStop(1, '#00FFFF');
|
96 |
-
|
97 |
let color2 = canvasCtx.createLinearGradient(0, 0, oW, oH);
|
98 |
color2.addColorStop(0, '#1E90FF');
|
99 |
color2.addColorStop(.25, '#FFD700');
|
100 |
color2.addColorStop(.5, '#8A2BE2');
|
101 |
color2.addColorStop(.75, '#4169E1');
|
102 |
color2.addColorStop(1, '#FF0000');
|
103 |
-
|
104 |
let output = new Uint8Array(180);
|
105 |
-
let du = 2;
|
106 |
-
let R = 200;
|
107 |
-
let W = 2;
|
108 |
(function drawSpectrum() {
|
109 |
analyser.getByteFrequencyData(output);
|
110 |
canvasCtx.clearRect(0, 0, wrap.width, wrap.height);
|
@@ -116,16 +102,13 @@ window.onload = function () {
|
|
116 |
Rv2 = (R + value);
|
117 |
canvasCtx.moveTo((Math.sin((i * du) / 180 * Math.PI) * Rv1 + oW / 2), -Math.cos((i * du) / 180 * Math.PI) * Rv1 + oH / 2);
|
118 |
canvasCtx.lineTo((Math.sin((i * du) / 180 * Math.PI) * Rv2 + oW / 2), -Math.cos((i * du) / 180 * Math.PI) * Rv2 + oH / 2);
|
119 |
-
canvasCtx.strokeStyle = color1
|
120 |
canvasCtx.stroke();
|
121 |
}
|
122 |
-
|
123 |
-
|
124 |
-
canvasCtx.
|
125 |
-
canvasCtx.
|
126 |
-
canvasCtx.textAlign = "center"; // 设置水平对齐方式
|
127 |
-
canvasCtx.textBaseline = "middle"; // 设置垂直对齐方式
|
128 |
-
// 绘制文字(参数:要写的字,x坐标,y坐标)
|
129 |
canvasCtx.fillText(file.name.split('.mp3')[0], oW / 2, oH / 2);
|
130 |
requestAnimationFrame(drawSpectrum);
|
131 |
})();
|
|
|
19 |
wrap.height = window.innerHeight - 1;
|
20 |
let canvasCtx = wrap.getContext("2d");
|
21 |
let AudioContext = window.AudioContext || window.webkitAudioContext || window.mozAudioContext;
|
22 |
+
let audioContext = new AudioContext();
|
|
|
|
|
23 |
canvasCtx.shadowColor = "white";
|
24 |
canvasCtx.shadowBlur = 10;
|
|
|
25 |
$('#bgFile').change(function () {
|
26 |
if (this.files.length <= 0) return;
|
|
|
27 |
if (this.files[0].size > 5242880) {
|
28 |
console.log('File size larger than 5M');
|
29 |
alert('Too large!');
|
30 |
return;
|
31 |
}
|
|
|
32 |
let file = this.files[0];
|
33 |
let reader = new FileReader();
|
34 |
reader.readAsDataURL(file);
|
|
|
38 |
});
|
39 |
|
40 |
$('#musicFile').change(function () {
|
|
|
|
|
41 |
if (audioContext.state == 'running') {
|
42 |
audioContext.close();
|
43 |
audioContext = new AudioContext();
|
44 |
}
|
|
|
45 |
if (this.files.length == 0) return;
|
46 |
let file = $('#musicFile')[0].files[0];
|
47 |
let fileReader = new FileReader();
|
48 |
fileReader.readAsArrayBuffer(file);
|
49 |
fileReader.onload = function (e) {
|
50 |
let count = 0;
|
51 |
+
$('#tip').text('Start decoding')
|
52 |
let timer = setInterval(function () {
|
53 |
count++;
|
54 |
+
$('#tip').text('In decoding, already used ' + count + ' s')
|
55 |
}, 1000)
|
56 |
audioContext.decodeAudioData(e.target.result, function (buffer) {
|
57 |
clearInterval(timer)
|
58 |
+
$('#tip').text('Decoded successfully, total time: ' + count + ' s');
|
59 |
let audioBufferSourceNode = audioContext.createBufferSource();
|
60 |
let analyser = audioContext.createAnalyser();
|
61 |
audioBufferSourceNode.connect(analyser);
|
62 |
analyser.connect(audioContext.destination);
|
63 |
audioBufferSourceNode.buffer = buffer;
|
64 |
audioBufferSourceNode.start();
|
|
|
|
|
65 |
startStop.onclick = function () {
|
66 |
if (audioContext.state === 'running') {
|
67 |
audioContext.suspend().then(function () {
|
68 |
+
$("#startStop").val('Play');
|
69 |
});
|
70 |
} else if (audioContext.state === 'suspended') {
|
71 |
audioContext.resume().then(function () {
|
72 |
+
$("#startStop").val('Pause');
|
73 |
});
|
74 |
}
|
75 |
}
|
76 |
let oW = wrap.width;
|
77 |
let oH = wrap.height;
|
|
|
|
|
78 |
let color1 = canvasCtx.createLinearGradient(oW / 2, oH / 2 - 10, oW / 2, oH / 2 - 150);
|
79 |
color1.addColorStop(0, '#1E90FF');
|
80 |
color1.addColorStop(.25, '#FF7F50');
|
81 |
color1.addColorStop(.5, '#8A2BE2');
|
82 |
color1.addColorStop(.75, '#4169E1');
|
83 |
color1.addColorStop(1, '#00FFFF');
|
|
|
84 |
let color2 = canvasCtx.createLinearGradient(0, 0, oW, oH);
|
85 |
color2.addColorStop(0, '#1E90FF');
|
86 |
color2.addColorStop(.25, '#FFD700');
|
87 |
color2.addColorStop(.5, '#8A2BE2');
|
88 |
color2.addColorStop(.75, '#4169E1');
|
89 |
color2.addColorStop(1, '#FF0000');
|
|
|
90 |
let output = new Uint8Array(180);
|
91 |
+
let du = 2;
|
92 |
+
let R = 200;
|
93 |
+
let W = 2;
|
94 |
(function drawSpectrum() {
|
95 |
analyser.getByteFrequencyData(output);
|
96 |
canvasCtx.clearRect(0, 0, wrap.width, wrap.height);
|
|
|
102 |
Rv2 = (R + value);
|
103 |
canvasCtx.moveTo((Math.sin((i * du) / 180 * Math.PI) * Rv1 + oW / 2), -Math.cos((i * du) / 180 * Math.PI) * Rv1 + oH / 2);
|
104 |
canvasCtx.lineTo((Math.sin((i * du) / 180 * Math.PI) * Rv2 + oW / 2), -Math.cos((i * du) / 180 * Math.PI) * Rv2 + oH / 2);
|
105 |
+
canvasCtx.strokeStyle = color1;
|
106 |
canvasCtx.stroke();
|
107 |
}
|
108 |
+
canvasCtx.font = "italic bold 20px Microsoft Yahei";
|
109 |
+
canvasCtx.fillStyle = color2;
|
110 |
+
canvasCtx.textAlign = "center";
|
111 |
+
canvasCtx.textBaseline = "middle";
|
|
|
|
|
|
|
112 |
canvasCtx.fillText(file.name.split('.mp3')[0], oW / 2, oH / 2);
|
113 |
requestAnimationFrame(drawSpectrum);
|
114 |
})();
|