Okay, so I've got something as simple as this:
<audio autoplay='autoplay' loop='loop' id='audio' controls>
<source src='http://www.w3schools.com/html/horse.ogg'/>
<source src='http://www.w3schools.com/html/horse.mp3'/>
</audio>
Now, when I play the audio on an iOS device, it loops fine and everything, however, when I close the browser window, or switch tabs, it just keeps on looping and the only way to pause it is if I shut the browser tab. I tried removing the loop='loop'
and replaying it onended
with JavaScript, and that works, but with audio that is about 1.5 mins long, it can still be pretty annoying. but that didn't work either because the ended
event fires even when the browser isn't open.
Demo in which the sound is the first one I could find online, but note that my actual sound is pretty long, so I do need to pause
it when the browser window is closed.
I've tried pausing it onblur
, onunload
and onbeforeunload
to no avail. Is there a way to stop the audio from playing in the background?