totoaussi
totoaussi

Reputation: 741

Audio in HTML5 : how to play a sound after an other sound?

I have three sound objects :

sound_a = new Audio("a.mp3");
sound_b = new Audio("b.mp3");
sound_c = new Audio("c.mp3");

I want to play the sound b when the sound a is finished. And play the sound c when the sound b is finish.

How I can do that ?

My code is the following :

sound_a.pause();
sound_a.currentTime = 0;
sound_a.play();

sound_b.pause();
sound_b.currentTime = 0;
sound_b.play();

sound_c.pause();
sound_c.currentTime = 0;
sound_c.play();

Upvotes: 0

Views: 73

Answers (2)

mido
mido

Reputation: 25034

the easiest way to do it would be, keep a single audio object, array of sources, listen to end event and play:

let i = -1, sources =[...], a= new Audio(), playNext= e => {
  i++;
  if(!sources[i]) return;
  a.src=sources[i];
}
a.autoplay = true; 
a.onended = playNext;
playNext();

Fiddle Demo

Upvotes: 1

Coke
Coke

Reputation: 985

You can find an answer for your question here. If that does not help, try to find other solutions, since this question was already asked and solved before.

Upvotes: 1

Related Questions