2019-05-11 23:23:27 +02:00
|
|
|
let gstate = "idle";
|
|
|
|
let repeat = false;
|
|
|
|
let continuelist = true;
|
|
|
|
let total = 0;
|
|
|
|
let index = 0;
|
|
|
|
|
|
|
|
let sound = new Howl({
|
2021-03-06 20:32:48 +01:00
|
|
|
src: [''],
|
|
|
|
format: "mp3",
|
|
|
|
html5: true
|
2019-05-11 23:23:27 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
setInterval(function () {
|
2021-03-06 20:32:48 +01:00
|
|
|
updateState();
|
2019-05-11 23:23:27 +02:00
|
|
|
}, 1000);
|
|
|
|
|
|
|
|
window.onload = function () {
|
2021-03-06 20:32:48 +01:00
|
|
|
initState();
|
|
|
|
updateState()
|
2019-05-11 23:23:27 +02:00
|
|
|
};
|
|
|
|
|
2021-03-05 20:59:37 +01:00
|
|
|
window.onkeyup = function (event) {
|
2021-03-06 20:32:48 +01:00
|
|
|
switch (event.key) {
|
|
|
|
case " ":
|
|
|
|
case "p":
|
|
|
|
if (gstate == "idle" && total !== 0) {
|
2021-03-06 22:52:55 +01:00
|
|
|
playSong(index)
|
2021-03-06 20:32:48 +01:00
|
|
|
} else {
|
|
|
|
togglePlayback();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "r":
|
|
|
|
toggleRepeat();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "c":
|
|
|
|
toggleContinue();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "ArrowUp":
|
|
|
|
previousTrack();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "ArrowDown":
|
|
|
|
nextTrack();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "ArrowLeft":
|
|
|
|
if (sound.seek() < 10) {
|
|
|
|
sound.seek(0);
|
|
|
|
} else {
|
|
|
|
sound.seek(sound.seek()-10);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "ArrowRight":
|
|
|
|
sound.seek(sound.seek()+10);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "Escape":
|
|
|
|
document.getElementById("back").click();
|
|
|
|
break;
|
|
|
|
}
|
2021-03-05 20:59:37 +01:00
|
|
|
};
|
2019-05-11 23:23:27 +02:00
|
|
|
|
2021-03-06 22:52:55 +01:00
|
|
|
function initState() {
|
|
|
|
const dirElements = document.querySelectorAll(".dir");
|
|
|
|
const fileElements = document.querySelectorAll(".file");
|
|
|
|
|
|
|
|
let id = 0;
|
|
|
|
|
|
|
|
dirElements.forEach(function(element){
|
|
|
|
element.id = id++;
|
|
|
|
});
|
|
|
|
|
|
|
|
fileElements.forEach(function(element){
|
|
|
|
element.id = id++;
|
|
|
|
|
|
|
|
element.addEventListener("click", function(event) {
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
playSong(event.target.id);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
total = id;
|
|
|
|
}
|
2019-05-11 23:23:27 +02:00
|
|
|
|
|
|
|
function togglePlayback() {
|
2021-03-06 20:36:43 +01:00
|
|
|
if (sound.playing()) {
|
2021-03-06 20:32:48 +01:00
|
|
|
sound.pause();
|
2021-03-06 20:36:43 +01:00
|
|
|
} else {
|
2021-03-06 20:32:48 +01:00
|
|
|
sound.play();
|
2021-03-06 20:36:43 +01:00
|
|
|
}
|
2019-05-11 23:23:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function setState(state) {
|
2021-03-06 20:32:48 +01:00
|
|
|
gstate = state;
|
2021-03-06 20:36:43 +01:00
|
|
|
|
2021-03-06 20:32:48 +01:00
|
|
|
console.log("now in state: " + state);
|
|
|
|
updateState();
|
2019-05-11 23:23:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-03-06 22:52:55 +01:00
|
|
|
function playSong(id) {
|
|
|
|
let element = document.getElementById(id);
|
|
|
|
|
|
|
|
if (element === null) return;
|
|
|
|
if (element.classList.contains('dir')) return;
|
|
|
|
|
2021-03-06 20:36:43 +01:00
|
|
|
if (document.getElementsByClassName("playing").length > 0) {
|
2021-03-06 20:32:48 +01:00
|
|
|
document.getElementsByClassName("playing")[0].classList.remove("playing");
|
2021-03-06 20:36:43 +01:00
|
|
|
}
|
|
|
|
|
2021-03-06 22:52:55 +01:00
|
|
|
index = element.id;
|
|
|
|
|
2021-03-06 20:32:48 +01:00
|
|
|
sound.stop();
|
|
|
|
sound.unload();
|
|
|
|
sound = null;
|
|
|
|
delete sound;
|
|
|
|
|
|
|
|
sound = new Howl({
|
2021-03-06 22:52:55 +01:00
|
|
|
src: [element.href],
|
2021-03-06 20:32:48 +01:00
|
|
|
html5: true
|
|
|
|
});
|
|
|
|
|
|
|
|
setState("loading");
|
|
|
|
sound.play();
|
|
|
|
sound.loop(repeat);
|
|
|
|
|
2021-03-06 22:52:55 +01:00
|
|
|
element.classList.add("playing");
|
2021-03-06 20:32:48 +01:00
|
|
|
|
|
|
|
sound.on("play", function () {
|
|
|
|
setState("playing");
|
|
|
|
});
|
|
|
|
sound.on("loaderror", function () {
|
|
|
|
setState("error loading track")
|
|
|
|
});
|
|
|
|
sound.on("playerror", function () {
|
|
|
|
setState("error opening audio device")
|
|
|
|
});
|
|
|
|
sound.on("end", function () {
|
|
|
|
setState("idle");
|
|
|
|
nextTrack()
|
|
|
|
});
|
|
|
|
sound.on("pause", function () {
|
|
|
|
setState("paused")
|
|
|
|
});
|
2019-05-11 23:23:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function toggleRepeat() {
|
2021-03-06 20:32:48 +01:00
|
|
|
repeat = !repeat;
|
|
|
|
continuelist = !repeat;
|
|
|
|
sound.loop(repeat);
|
|
|
|
updateState();
|
2019-05-11 23:23:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function toggleContinue() {
|
2021-03-06 20:32:48 +01:00
|
|
|
continuelist = !continuelist;
|
|
|
|
updateState();
|
2019-05-11 23:23:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function updateState() {
|
2021-03-06 20:32:48 +01:00
|
|
|
document.getElementById("state").setAttribute('onclick', 'togglePlayback()');
|
|
|
|
let statestr = "[";
|
|
|
|
statestr += gstate;
|
2021-03-06 20:36:43 +01:00
|
|
|
if (sound.playing()) {
|
2021-03-06 20:32:48 +01:00
|
|
|
statestr += " " + formatTime(Math.round(sound.seek())) + "/" + formatTime(Math.round(sound.duration()));
|
2021-03-06 20:36:43 +01:00
|
|
|
}
|
2021-03-06 20:32:48 +01:00
|
|
|
|
|
|
|
statestr += "]";
|
|
|
|
document.getElementById("state").innerHTML = statestr;
|
|
|
|
let flags = "[";
|
|
|
|
if (repeat)
|
|
|
|
flags += '<a onclick="toggleRepeat()" href="#" style="color:#6b9969">R</a>';
|
|
|
|
else
|
|
|
|
flags += '<a onclick="toggleRepeat()" href="#">R</a>';
|
|
|
|
if (continuelist)
|
|
|
|
flags += '<a onclick="toggleContinue()" href="#" style="color:#6b9969">C</a>';
|
|
|
|
else
|
|
|
|
flags += '<a onclick="toggleContinue()" href="#">C</a>';
|
|
|
|
|
|
|
|
document.getElementById("flags").innerHTML = flags + "]";
|
2019-05-11 23:23:27 +02:00
|
|
|
}
|
|
|
|
|
2021-03-05 23:10:43 +01:00
|
|
|
function previousTrack() {
|
2021-03-06 20:36:43 +01:00
|
|
|
if (index-- === 0) index = total-1;
|
|
|
|
|
2021-03-06 20:32:48 +01:00
|
|
|
if (continuelist) {
|
2021-03-06 22:52:55 +01:00
|
|
|
playSong(index)
|
2021-03-06 20:32:48 +01:00
|
|
|
}
|
2021-03-05 23:10:43 +01:00
|
|
|
}
|
|
|
|
|
2019-05-11 23:23:27 +02:00
|
|
|
function nextTrack() {
|
2021-03-06 20:36:43 +01:00
|
|
|
if (++index === total) index = 0;
|
|
|
|
|
2021-03-06 20:32:48 +01:00
|
|
|
if (continuelist) {
|
2021-03-06 22:52:55 +01:00
|
|
|
playSong(index)
|
2021-03-06 20:32:48 +01:00
|
|
|
}
|
2019-05-11 23:23:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function formatTime(secs) {
|
2021-03-06 20:32:48 +01:00
|
|
|
const minutes = Math.floor(secs / 60) || 0;
|
|
|
|
const seconds = (secs - minutes * 60) || 0;
|
|
|
|
return minutes + ':' + (seconds < 10 ? '0' : '') + seconds;
|
2021-03-06 14:47:36 +01:00
|
|
|
}
|