Skip to content Skip to sidebar Skip to footer

2 Audio Sounds And I Want To Play One Html5 Audio Element At A Time

Question: I have 2 audio sounds and I want to play one HTML5 audio element at a time. Similar Post: Play one HTML audio element at a time. I saw a similar post to my question. H

Solution 1:

I think the problem is you are listing to click event, when you should be listening to play event, also for some reason, window.onload does not work properly in jsfiddle, so replace

window.onload = function () {
    $("sound1").onclick = sound1_click;
    $("sound2").onclick = sound2_click;
}

with

$("sound1").onplay = sound1_click;
$("sound2").onplay = sound2_click;

fiddle demo

or you can simply generalize it by:

var audios = document.getElementsByTagName('audio');
for(var i=0; i<audios.length;i++)    audios[i].onplay = pauseAllAudios.bind(null, audios[i]);

functionpauseAllAudios(audio){
    for(var i=0; i<audios.length;i++)
        if(audios[i]!=audio)    audios[i].pause();    
};

fiddle demo

Post a Comment for "2 Audio Sounds And I Want To Play One Html5 Audio Element At A Time"