-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmusicplayer.js
More file actions
137 lines (106 loc) · 3.45 KB
/
Copy pathmusicplayer.js
File metadata and controls
137 lines (106 loc) · 3.45 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
const audio = document.getElementById("audio");
const playBtn = document.getElementById("play");
const seek = document.getElementById("seek");
const vol = document.getElementById("vol");
const cur = document.getElementById("cur");
const dur = document.getElementById("dur");
const eq = document.getElementById("eq");
const canvasElt = document.getElementById("visualizer");
let audioContext, analyserNode, sourceNode;
let rafId = null;
if (!audio || !playBtn || !seek || !cur || !dur) {
console.error("Player missing elements:", { audio, playBtn, seek, cur, dur, vol });
}
function fmt(t){
if (!isFinite(t)) return "0:00";
const m = Math.floor(t/60);
const s = Math.floor(t%60).toString().padStart(2,"0");
return `${m}:${s}`;
}
function setEqState(){
if (!eq) return;
eq.classList.toggle("idle", audio.paused);
eq.classList.toggle("playing", !audio.paused);
}
function initVisualizer() {
if (!canvasElt) return;
if (!audioContext) {
audioContext = new (window.AudioContext || window.webkitAudioContext)();
analyserNode = audioContext.createAnalyser();
analyserNode.fftSize = 1024;
analyserNode.smoothingTimeConstant = 0.85;
sourceNode = audioContext.createMediaElementSource(audio);
sourceNode.connect(analyserNode);
analyserNode.connect(audioContext.destination);
}
}
function startVisualizer() {
if (!canvasElt) return;
initVisualizer();
if (audioContext.state === "suspended") audioContext.resume();
if (rafId) return;
const canvasContext = canvasElt.getContext("2d");
console.log("canvas context get");
const width = canvasElt.width;
const height = canvasElt.height;
function draw() {
const freq = new Uint8Array(analyserNode.frequencyBinCount);
analyserNode.getByteTimeDomainData(freq);
rafId = requestAnimationFrame(draw);
canvasContext.clearRect(0,0, width, height);
console.log("canvas cleared");
const arrlen = freq.length;
console.log("drawing");
for (let i = 0; i < arrlen; i++) {
const value = Math.min(1, Math.max(0, freq[i] / 255));
const y = height - (height*value);
const x = (i/(arrlen - 1))*(width - 1);
if (i%2==0) {canvasContext.fillStyle="red"}
else {canvasContext.fillStyle = "blue";}
canvasContext.fillRect(x, y, 5, 6);
}
}
draw();
}
function stopVisualizer() {
if (rafId) cancelAnimationFrame(rafId);
rafId = null;
}
playBtn.addEventListener("click", async () => {
if (audio.paused) { await audio.play(); }
else { audio.pause(); }
});
audio.addEventListener("play", () => {
playBtn.textContent = "pause";
setEqState();
startVisualizer();
});
audio.addEventListener("pause", () => {
playBtn.textContent = "play";
setEqState();
stopVisualizer();
});
audio.addEventListener("ended", () => {
setEqState();
stopVisualizer();
});
audio.addEventListener("loadedmetadata", () => {
seek.max = audio.duration;
dur.textContent = fmt(audio.duration);
});
audio.addEventListener("timeupdate", () => {
if (!seek.matches(":active")) seek.value = audio.currentTime;
cur.textContent = fmt(audio.currentTime);
});
seek.addEventListener("input", () => {
audio.currentTime = Number(seek.value);
});
if (vol) {
vol.addEventListener("input", () => {
audio.volume = Number(vol.value);
});
audio.volume = Number(vol.value);
} else {
audio.volume = 1;
}
setEqState();