简体   繁体   English

html5 / javascript音频同时播放多个曲目

[英]html5/javascript audio play multiple tracks at the same time

Is it possible to play the whole audio instance at the same time? 是否可以同时播放整个音频实例?
I know, I could create every time an new instance and call .play() on every instance, but I think its dirty. 我知道,我可以每次创建一个新实例并在每个实例上调用.play(),但我认为它很脏。 like 喜欢
sound1 = new Audio('sound1.mp3');
sound2 = new Audio('sound2.mp3);
sound1.play();
sound2.play;

Its more or less the same as in this thread: Play multiple sound at the same time 它或多或少与此线程相同:同时播放多个声音
But there has to be and elegant way? 但必须有优雅的方式吗?

Edit: MediaController and mediagroup turned out to be vaporware. 编辑: MediaControllermediagroup结果是mediagroup You will want to stick with the "dirty" way to do it, at least for now, as there is no official syncing solution. 你会想要坚持“肮脏”的方式去做,至少现在,因为没有正式的同步解决方案。

The proper way to play multiple audio or video objects synchronously is to use the mediagroup attribute and the corresponding MediaController object. 同步播放多个音频或视频对象的正确方法是使用mediagroup属性和相应的MediaController对象。 See the article "HTML5 multi-track audio or video" : 请参阅文章“HTML5多轨音频或视频”

[A]n attribute called @mediagroup can be added in markup to slave multiple media elements together. 可以在标记中添加[A] n属性@mediagroup ,以将多个媒体元素连接在一起。 This is administrated in the JavaScript API through a MediaController object, which provides events and attributes for the combined multi-track object. 这是通过MediaController对象在JavaScript API中管理的,该对象为组合的多轨道对象提供事件和属性。

Set both audio files to a single mediagroup . 将两个音频文件都设置为单个mediagroup When a media element is assigned to a mediagroup , a MediaController is created for that group. 当媒体元素被分配给mediagroup ,一个MediaController是为该组创建。 Use the play() method on that controller and all media elements in the group will play synchronously. 在该控制器上使用play()方法,组中的所有媒体元素将同步播放。

Programmatically, you can assign a mediagroup like so, and then from that point trigger play() on the MediaController from any one of the slave media elements, like so: 以编程方式,您可以分配这样的mediagroup ,然后从该点触发MediaController任何一个从属媒体元素的play() ,如下所示:

 sound1 = new Audio('sound1.mp3'); sound2 = new Audio('sound2.mp3'); sound1.mediaGroup = 'soundGroup'; sound2.mediaGroup = 'soundGroup'; sound1.controller.play(); 

(Sources: w3school.com's HTML Audio/Video DOM mediaGroup Property page , controller Property page , play() Method page , and the standard ) (来源:w3school.com的HTML音频/视频DOM 媒体组属性页面控制器属性页面播放()方法页面标准

NOTE: I haven't tested the above code; 注意:我没有测试上面的代码; please, if you can confirm that this code is not standard-compliant and does not function as expected, let us know. 请注意,如果您确认此代码不符合标准且无法按预期运行,请告知我们。 Be aware that you are looking at a very new aspect of the HTML5 standard, and this being the case, do not expect broad browser compatibility. 请注意,您正在研究HTML5标准的一个非常新的方面,在这种情况下, 不要期望广泛的浏览器兼容性。 (as of January 2014) (截至2014年1月)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM