HTML5 JS plays the same sound multiple times at the same time

I am creating a very basic HTML5 game and I hit a little wall, basically I have an array, this array is filled with one sound file (this is an array so I can assign more sounds if needed)

this.Sounds["L_Explosion1"] = new Audio("Assets/Sounds/Explosion_1.wav");
this.Sounds["L_Explosion1"].volume = 1;
this.Sounds["L_Explosion1"].load();

      

When the player presses spacebar, I want to play a sound, so I run this code

this.Sounds["L_Explosion1"].play();

      

however, if I press spacebar again before playing the sound, it won't play it, what I'm trying to do is play the same sound multiple times, if the same hasn't ended yet, how would I achieve this?

+3


source to share


1 answer


To play multiple instances of sound at the same time, you need to create a new copy of this element Audio

. One way to do this is to use the method .cloneNode()

and play the cloned sound every time the spacebar is pressed.



this.Sounds["L_Explosion1"].cloneNode(true).play();

      

+5


source







All Articles