Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML5 JS play same sound multiple times at the same time

I'm create a very basic HTML5 game and I've hit a little wall, basically I have an array, this array is populated with one sound file (it 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 the space bar I want to play the sound so I run this code

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

however if I press space bar again before the sound played it will not play it, what I am trying to do is play the same sound multiple times event if the same has not yet finished, how would I achieve this?

like image 234
Canvas Avatar asked Sep 03 '14 22:09

Canvas


1 Answers

In order to play multiple instances of the sound at the same time, you will need to create a new copy of that Audio element. One way to do this is to use the .cloneNode() method and play the cloned audio every time the spacebar is pressed.

this.Sounds["L_Explosion1"].cloneNode(true).play();
like image 181
Tanuj Mathur Avatar answered Oct 05 '22 23:10

Tanuj Mathur