2

I'm trying to get my website to play multiple mp3/songs, but it keeps playing soundtrack1, but I want it to play all 3 songs at the same time.

This is what I currently have:

<audio loop autoplay controls>
    <source src="soundtrack1.mp3" type="audio/mpeg" loop="true">
    <source src="soundtrack2.mp3" type="audio/mpeg" loop="true">
    <source src="soundtrack3.mp3" type="audio/mpeg" loop="true">
    Your browser does not support the audio element.
</audio>
Sainan
  • 1,274
  • 2
  • 19
  • 32
NaserIT
  • 41
  • 1
  • 3

2 Answers2

3

So the HTML is as easy 1, 2, 3

<audio src="..." id="song1" volume="0.1" style="display: none;"></audio>
<audio src="..." id="song2" volume="0.1" style="display: none;"></audio>
<audio src="..." id="song3" volume="0.1" style="display: none;"></audio>

And the JS magic behind it:

for(var i = 1; i <= 3; i++)
{
    document.querySelector("#song" + i)[0].play();
}
Sainan
  • 1,274
  • 2
  • 19
  • 32
1

If you want to do it in only Javascript:

var song1  = new Audio();
var src1  = document.createElement("source");
src1.type = "audio/mpeg";
src1.src  = "soundtrack1.mp3";
song1.appendChild(src1);
song1.play();

var song2  = new Audio();
var src2  = document.createElement("source");
src2.type = "audio/mpeg";
src2.src  = "soundtrack2.mp3";
song2.appendChild(src2);
song2.play();
Sainan
  • 1,274
  • 2
  • 19
  • 32
Rinkal Garg
  • 62
  • 1
  • 10