admin管理员组文章数量:1296921
Let me start by saying I am trying to play a mp3 file whenever the player collides with a wall, simple enough and done. But I wish to have another sound file play when the player hits another wall.
I have tried using both <audio>
tag in the html file, than playing it in Javascript:
var sound = document.getElementById("TheHtml5Tag");
Still the same issue, only one sound can be played, the next sound won't play untill the first one is finished. Even when using multiple <audio>
tags and multiple sounds.
Next I have tried using codes similar too:
sound = new Audio('Wall_Hit_01.mp3');
sound.addEventListener('ended', function() {
this.currentTime = 0;
}, false);
Than playing it with: sound.play();
But still the same issue: I can't play sound2.play();
or sound3.play();
before the first sound has finished.
Any answers/ suggestions are much appreciated. If there isn't a way to do this on every browser maybe some tips on how too stop all the sounds in order to play a new sound would be nice. Though I would much rather go with the original question.
Let me start by saying I am trying to play a mp3 file whenever the player collides with a wall, simple enough and done. But I wish to have another sound file play when the player hits another wall.
I have tried using both <audio>
tag in the html file, than playing it in Javascript:
var sound = document.getElementById("TheHtml5Tag");
Still the same issue, only one sound can be played, the next sound won't play untill the first one is finished. Even when using multiple <audio>
tags and multiple sounds.
Next I have tried using codes similar too:
sound = new Audio('Wall_Hit_01.mp3');
sound.addEventListener('ended', function() {
this.currentTime = 0;
}, false);
Than playing it with: sound.play();
But still the same issue: I can't play sound2.play();
or sound3.play();
before the first sound has finished.
Any answers/ suggestions are much appreciated. If there isn't a way to do this on every browser maybe some tips on how too stop all the sounds in order to play a new sound would be nice. Though I would much rather go with the original question.
- Did you manage to work this out perhaps? I have similar issues with sounds in browser still … If you have please consider answering my question here stackoverflow./questions/23006776/… – trainoasis Commented Apr 15, 2014 at 11:20
- See my question and answer here: stackoverflow./questions/16360874/… stackoverflow./questions/23006776/… – trainoasis Commented May 7, 2014 at 7:40
1 Answer
Reset to default 4I found a pretty neat solution that utilizes pure JavaScript.
In my HTML File, I put two audio elements and a play audio buttons in the same div.
<div id="audioplay">
<audio id='audio1' src="ex1.wav" type="audio/wav" preload="auto" autobuffer controls ></audio>
<audio id='audio2' src="ex2.wav" type="audio/wav" preload="auto" autobuffer controls></audio>
<button id='playaudio' onclick='playAudio()'>Play Audio</button>
In my JS file (you could use a script tag too), I put this code, which plays both of the files. Make sure that your audio src tags point to the right place!
function playAudio(){
var audio1 = document.getElementById('audio1');
var audio2 = document.getElementById('audio2');
audio1.play();
audio2.play();
}
本文标签: htmlJavascripthtml5 play multiple audio files or overlap audioStack Overflow
版权声明:本文标题:html - Javascripthtml5 play multiple audio files or overlap audio - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741640543a2389891.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论