I used the following code to add sound to my HTML document.
function sound(src) {
this.sound = document.createElement("audio");
this.sound.src = src;
this.sound.setAttribute("preload", "auto");
this.sound.setAttribute("controls", "none");
this.sound.style.display = "none";
document.body.appendChild(this.sound);
this.play = function(){
this.sound.play();
}
this.stop = function(){
this.sound.pause();
}
}
var bite = new sound("Bite+2.mp3");
var slurp = new sound("Slurp+4.mp3");
var hiss = new sound("snakehiss2.mp3");
var bg = new sound("gameSound.mp3");
Now, I want to set the volume of 'bg' and make its loop = true
I can't figure out a way to do this. Please help!!
Also, I saw the code somewhere on the net, and I can't understand how 'this.sound' is working. Please explain
Edit:
Turns out, I only needed to do bg.sound.volume = 0.5 to do that. Similarly, bg.sound.loop = true.
I also understand the use of 'this', but why is 'sound' required? Wouldn't this.src suffice instead of this.sound.src?