-
-
Save werelax/a5609a48a15403c1e06994bff44f41b5 to your computer and use it in GitHub Desktop.
Unlock Web Audio in iOS 9 Safari
This file contains 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