multiple audio html : auto stop other when current is playing with javascript

后端 未结 6 852
情深已故
情深已故 2020-12-13 02:58

I have 10 audio players with simple html audio tags on a html5 page. No jquery, no special audio js plugins, etc...

Does anyone has a simple script in js to pause al

6条回答
  •  生来不讨喜
    2020-12-13 03:12

    Mixing both previous answers that didn't work, i've used that. I just added && window.$_currentlyPlaying != evt.target and all is working. Also i've created a gist with this and other goodies for audio tags. javascript-audio-tags

    window.addEventListener("play", function(evt)
    {
        if(window.$_currentlyPlaying && window.$_currentlyPlaying != evt.target)
        {
            window.$_currentlyPlaying.pause();
        } 
        window.$_currentlyPlaying = evt.target;
    }, true);
    

提交回复
热议问题