Files
infinifi/web/script.js
Kenneth 66a873b3c2 fix: prevent browser from caching audio
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
2024-07-20 21:48:43 +01:00

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();
}
};