Add a random query parameter to the end of the track URL to prevent the browser from caching the audio file, forcing it to fetch the latest track
36 lines
659 B
JavaScript
36 lines
659 B
JavaScript
const playBtn = document.getElementById("play-btn");
|
|
|
|
let isPlaying = false;
|
|
let currentAudio;
|
|
let volume = 1;
|
|
|
|
function playAudio() {
|
|
// add a random query parameter at the end to prevent browser caching
|
|
currentAudio = new Audio(`/current.mp3?t=${Date.now()}`);
|
|
currentAudio.onplay = () => {
|
|
isPlaying = true;
|
|
playBtn.innerText = "pause";
|
|
};
|
|
currentAudio.onpause = () => {
|
|
isPlaying = false;
|
|
playBtn.innerText = "play";
|
|
};
|
|
|
|
currentAudio.volume = volume;
|
|
|
|
currentAudio.load();
|
|
currentAudio.play();
|
|
}
|
|
|
|
function pauseAudio() {
|
|
currentAudio.pause();
|
|
}
|
|
|
|
playBtn.onclick = () => {
|
|
if (isPlaying) {
|
|
pauseAudio();
|
|
} else {
|
|
playAudio();
|
|
}
|
|
};
|