I have this script and I want to ask if it's good/optimal and if it's not how should I change it?
What it's doing is when the website loads music, it starts to play when the user clicks prevent play() not running because user didn't interact) from an array of music and when a music stops a new one plays.
In the future I will have many songs in the array. I thought maybe I could change it so it selects from a folder (like a physhical folder in Windows) is this possible?
console.logs for debugging
var music = document.getElementById("BackgroundM");
var source = document.getElementById("source");
var musicArray = [
"/music/Cruel.mp3",
"/music/Ragnarok.mp3",
"/music/Berserk.mp3",
"/music/Wrath.mp3",
"/music/Immaculate.mp3",
"/music/BassSlut.mp3"
]
music.volume = 0.05;
var musicSelect = function() {
if(music.paused) {
var rand = Math.floor(Math.random() * musicArray.length)
musicArray[rand]
source.src = musicArray[rand]
music.load();
music.play();
console.log("Playing " + source.src)
}
}
document.addEventListener('click', function() {
console.log(music.paused)
musicSelect()
})
music.addEventListener('pause', function(){
console.log("Music is paused")
musicSelect()
})