javascript 同时播放多个声音

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11652197/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 13:54:54  来源:igfitidea点击:

Play multiple sound at the same time

javascripthtml

提问by Polymorphism

I want using HTML 5 multiple sound play how do it ?

我想用HTML 5 多声音播放怎么办?

<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>

采纳答案by Lasse Christiansen

With JavaScript and the HTML5 Audio API you could do something like this:

使用 JavaScript 和 HTML5 Audio API,您可以执行以下操作:

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(); // Now both will play at the same time

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

我已经在 Chrome v. 20 中对此进行了测试,它似乎有效:)

The <source>tag is used to specify different formats of the same file - such that the browser can choose another format as fallback in case the first one is not supported. That is why your own suggested solution does not work.

<source>标签用于指定同一文件的不同格式 - 这样浏览器可以选择另一种格式作为后备,以防第一种格式不受支持。这就是您自己建议的解决方案不起作用的原因。