-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscripts.js
38 lines (29 loc) · 1.11 KB
/
scripts.js
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
const AudioContext = window.AudioContext;
const audioCtx = new AudioContext();
const audioElement = document.querySelector("audio");
const playBtn = document.querySelector("button");
const volumeSlider = document.querySelector(".volume");
const audioSource = audioCtx.createMediaElementSource(audioElement);
playBtn.addEventListener("click", () => {
if (audioCtx.state === "suspended") {
audioCtx.resume();
}
if (playBtn.getAttribute("class") == "paused") {
audioElement.play();
playBtn.setAttribute("class", "playing");
playBtn.textContent = "Pause";
} else if (playBtn.getAttribute("class") == "playing") {
audioElement.pause();
playBtn.setAttribute("class", "paused");
playBtn.textContent = "Play";
}
audioElement.addEventListener("ended", () => {
playBtn.setAttribute("class", "paused");
playBtn.textContent = "Play";
});
});
const gainNode = audioCtx.createGain();
volumeSlider.addEventListener("input", () => {
gainNode.gain.value = volumeSlider.value;
});
audioSource.connect(gainNode).connect(audioCtx.destination);