I'm creating a Canvas game and I'm using this to stop all audios:
document.querySelectorAll("audio").forEach((el) => {
if (
el.id === "song1" ||
el.id === "song2" ||
el.id === "song3"
) {
el.volume = 0;
el.pause();
} else {
el.currentTime = 0;
el.volume = 0;
el.pause();
}
});
The problem I have is that this stop all the audios within the <head></head> tag in the html file (preloaded ones), but it does not work for audios created dinamically
function soundGo(newAudio, boolean) {
if (boolean === true) {
let audio = new Audio(newAudio);
audio.volume = 0.1;
audio.play();
} else {
let audio = new Audio(newAudio);
audio.volume = 0;
audio.pause();
}
}
I want to know if it's possible to iterate all the elements created with new Audio(). The querySelectorAll does not the trick, at least in my case (maybe I'm doing it wrong...).
I tried this also: stop dynamically created Audio