同时HTML5 / JavaScript的音频播放多音轨 [英] html5/javascript audio play multiple tracks at the same time

查看:2558
本文介绍了同时HTML5 / JavaScript的音频播放多音轨的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能在同一时间播放整个音频实例?结果
我知道,我可以创建每次一个新的实例,并调用.play()上的每个实例,但我认为它脏。
类似结果
= Sound1例子的新音乐('sound1.mp3');结果
SOUND2 =新的音频('sound2.mp3);结果
sound1.play();结果
sound2.play;

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

推荐答案

要同步播放多种音频或视频对象,正确的方法是使用 MEDIAGROUP 属性和相应的的MediaController 对象。请参阅文章HTML5多轨音频或视频

The proper way to play multiple audio or video objects synchronously is to use the mediagroup attribute and the corresponding MediaController object. See the article "HTML5 multi-track audio or video":

[A] n属性名为<一个href=\"http://www.whatwg.org/specs/web-apps/current-work/multipage/the-video-element.html#attr-media-mediagroup\">@mediagroup在标记被添加到从多种媒体元素结合在一起。这是通过一<一个给药JavaScript API中href=\"http://www.whatwg.org/specs/web-apps/current-work/multipage/the-video-element.html#synchronising-multiple-media-elements\">MediaController对象,它提供的事件和属性组合的多轨对象。

[A]n attribute called @mediagroup can be added in markup to slave multiple media elements together. This is administrated in the JavaScript API through a MediaController object, which provides events and attributes for the combined multi-track object.

两个音频文件设置为一个 MEDIAGROUP 。当媒体元素被分配到 MEDIAGROUP ,一个的MediaController 为组创建。使用播放控制器()方法和组中的所有媒体内容将同步播放。

Set both audio files to a single mediagroup. When a media element is assigned to a mediagroup, a MediaController is created for that group. Use the play() method on that controller and all media elements in the group will play synchronously.

编程,您可以在指定 MEDIAGROUP 像这样,然后从该点触发播放() 的MediaController 来自从媒体元素中的任何一个,像这样:

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:

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

(来源:w3school.com的HTML音频/视频DOM MEDIAGROUP属性页,的控制器属性页面 play()方法页面,而<一href=\"http://www.whatwg.org/specs/web-apps/current-work/multipage/the-video-element.html#synchronising-multiple-media-elements\">the标准)

注:我没有测试过上述code;请,如果你能确认这code是不符合标准的,不正常使用,让我们知道。要知道,你正在看HTML5标准的一个非常新的方面,这既然如此,不要指望广泛的浏览器兼容性。 (截至2014年1月)

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. (as of January 2014)

这篇关于同时HTML5 / JavaScript的音频播放多音轨的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆