问题
There are different solutions for the issue when you go to the background in the iPhone or iPad and the sound continuous playing, the most of them for the HMTL5 audio tag, but are not relevant if you are using Web Audio API because there are not an event like "timeupdate" and is a different concept of course. The Page Visibility API works in iOS7 only if you change of tab, but doesn't if you go to the background, in iOS6 not at all.
Someone knows any way to stop/mute a sound using Web Audio API if you go to the background in iOS6 or iOS7?
回答1:
To detect when safari is going background, you can use window's events called pageshow
and pagehide
(but this you already found out).
document.addEventListener('pageshow',function(){
// Do something here
}, false);
document.addEventListener('pagehide',function(){
// Do something here
}, false);
You can also use the PageVisibility API (available since iOS7) to check if the tab has changed.
document.addEventListener('visibilitychange', function(){
if (document.hidden) {
// Tab out of focus
}
else {
// Tab on focus
}
},false);
Note that this code should work on safari since iOS7, but some browsers needs prefixes.
回答2:
Auto-solved 80%. These are the unique events triggered when you go to the background and after that return to Safari, tested on both iOS.
window.addEventListener("pageshow", function(evt){
//fooBarCode
}, false);
window.addEventListener("pagehide", function(evt){
//fooBarCode
}, false);
This works only when you go to the background, but when you change of tab isn't a valid solution, any ideas?
来源:https://stackoverflow.com/questions/20472442/ios6-7-stop-sound-going-to-background-using-web-audio-api