-
-
Save zhishaofei3/f8f994fe4f5d09a47c35ffecb1100f49 to your computer and use it in GitHub Desktop.
Unlock Web Audio in iOS 9 Safari
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var ctx = null, usingWebAudio = true; | |
try { | |
if (typeof AudioContext !== 'undefined') { | |
ctx = new AudioContext(); | |
} else if (typeof webkitAudioContext !== 'undefined') { | |
ctx = new webkitAudioContext(); | |
} else { | |
usingWebAudio = false; | |
} | |
} catch(e) { | |
usingWebAudio = false; | |
} | |
// context state at this time is `undefined` in iOS8 Safari | |
if (usingWebAudio && ctx.state === 'suspended') { | |
var resume = function () { | |
ctx.resume(); | |
setTimeout(function () { | |
if (ctx.state === 'running') { | |
document.body.removeEventListener('touchend', resume, false); | |
} | |
}, 0); | |
}; | |
document.body.addEventListener('touchend', resume, false); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment