search

Home  >  Q&A  >  body text

Play multiple sounds at the same time

<p>Using HTML 5, I want to play multiple sounds at the same time. How do I do this? </p> <p>Here is my current code: </p> <pre class="brush:html;toolbar:false;"><audio controls="controls"> <source src="audio/(TESBIHAT).mp3" type="audio/mpeg" /> <source src="audio/Dombra.mp3" type="audio/mpeg" /> <embed height="50px" width="100px" src="audio/(TESBIHAT).mp3" /> <embed height="50px" width="100px" src="audio/Dombra.mp3" /> </audio> </pre>
P粉832212776P粉832212776509 days ago632

reply all(1)I'll reply

  • P粉682987577

    P粉6829875772023-08-14 12:28:59

    Using JavaScript and the HTML5 Audio API, you can do something like this:

    var snd1  = new Audio();
    var src1  = document.createElement("source");
    src1.type = "audio/mpeg";
    src1.src  = "audio/Dombra.mp3";
    snd1.appendChild(src1);
    
    var snd2  = new Audio();
    var src2  = document.createElement("source");
    src2.type = "audio/mpeg";
    src2.src  = "audio/(TESBIHAT).mp3";
    snd2.appendChild(src2);
    
    snd1.play(); snd2.play(); // 现在两者将同时播放

    I tested in Chrome v. 20 and it seems to work :)

    The

    <source> tag is used to specify different formats of the same file - this way the browser can choose another format as a fallback if the first format is not supported. That's why your own proposed solution doesn't work.

    reply
    0
  • Cancelreply