webmusic.js: code improovements

This commit is contained in:
Leah 2021-03-06 20:36:43 +01:00 committed by Leah (ctucx)
parent 81b67db9a1
commit 822996211f

View file

@ -67,22 +67,26 @@ window.onkeyup = function (event) {
function togglePlayback() { function togglePlayback() {
if (sound.playing()) if (sound.playing()) {
sound.pause(); sound.pause();
else } else {
sound.play(); sound.play();
}
} }
function setState(state) { function setState(state) {
gstate = state; gstate = state;
console.log("now in state: " + state); console.log("now in state: " + state);
updateState(); updateState();
} }
function playSong(url) { function playSong(url) {
if (document.getElementsByClassName("playing").length > 0) if (document.getElementsByClassName("playing").length > 0) {
document.getElementsByClassName("playing")[0].classList.remove("playing"); document.getElementsByClassName("playing")[0].classList.remove("playing");
}
index = queue.indexOf(url); index = queue.indexOf(url);
sound.stop(); sound.stop();
sound.unload(); sound.unload();
@ -134,8 +138,9 @@ function updateState() {
document.getElementById("state").setAttribute('onclick', 'togglePlayback()'); document.getElementById("state").setAttribute('onclick', 'togglePlayback()');
let statestr = "["; let statestr = "[";
statestr += gstate; statestr += gstate;
if (sound.playing()) if (sound.playing()) {
statestr += " " + formatTime(Math.round(sound.seek())) + "/" + formatTime(Math.round(sound.duration())); statestr += " " + formatTime(Math.round(sound.seek())) + "/" + formatTime(Math.round(sound.duration()));
}
statestr += "]"; statestr += "]";
document.getElementById("state").innerHTML = statestr; document.getElementById("state").innerHTML = statestr;
@ -157,16 +162,16 @@ function initState() {
} }
function previousTrack() { function previousTrack() {
if (index-- === 0) if (index-- === 0) index = total-1;
index = total-1;
if (continuelist) { if (continuelist) {
playSong(queue[index]) playSong(queue[index])
} }
} }
function nextTrack() { function nextTrack() {
if (++index === total) if (++index === total) index = 0;
index = 0;
if (continuelist) { if (continuelist) {
playSong(queue[index]) playSong(queue[index])
} }