简体   繁体   中英

Pause a sound when user leaves the page on mobile Safari

I am playing audio in Background of an application but I want to stop when user minimises or Safari goes to background.

I have already tried working element.onfocus .

I also tried to use window.requestAnimFrame but its results are not satisfactory.

I can't figure out how to stop the audio.

you might have success with the pageshow and pagehide events, which are triggered when safari goes to the background.

however there is no event that i know of, when you switch tabs within safari.

also here is a small library that does most of the work for you: https://github.com/zynga/jukebox

As answer to you and to Mageek comment, you should check the onfocus event and the onblur event (it is possible) prefered using jQuery :

$(window).focus(function() { 
//enable sound
}); 

$(window).blur(function() { 
//disable sound
}); 

This will probably do the trick, but havn't tested it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM