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?
The proper way to play multiple audio or video objects synchronously is to use the
mediagroup
attribute and the correspondingMediaController
object. See the article "HTML5 multi-track audio or video":Set both audio files to a single
mediagroup
. When a media element is assigned to amediagroup
, aMediaController
is created for that group. Use theplay()
method on that controller and all media elements in the group will play synchronously.Programmatically, you can assign a
mediagroup
like so, and then from that point triggerplay()
on theMediaController
from any one of the slave media elements, like so:(Sources: w3school.com's HTML Audio/Video DOM mediaGroup Property page, controller Property page, play() Method page, and the standard)
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)