Aswin G
Aswin G

Reputation: 401

Is it possible to layer sounds in JavaScript?

So I was making a program in javscript, and I was wondering if there is a way to 'layer' sounds in javascript, i.e, to make them play simultaneously.

I use the following code to play sound:

var snd=new Audio();
snd.src="sound.mp3";
//Code to ensure this and other assets load etc etc
//Playing sound
snd.play();

I've noticed if snd.play()executes, say, 3 times a second, it will play it once and wait till it stops playing, instead of playing it whenever snd.play() is called, even if it means having to play the same sound file simultaneously. How can I solve this?

Upvotes: 0

Views: 682

Answers (1)

jlb
jlb

Reputation: 20010

Create a new Audio object each time you want to play the same sound clip (if its already playing).

function playSound() {
   var snd = new Audio();
   snd.src = "sound.mp3";
   snd.play();
}

Upvotes: 1

Related Questions