0

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();
totoaussi
  • 712
  • 1
  • 11
  • 27

2 Answers2

1

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.

Community
  • 1
  • 1
Coke
  • 965
  • 2
  • 9
  • 22
1

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

mido
  • 24,198
  • 15
  • 92
  • 117