Skip to content

Instantly share code, notes, and snippets.

@werelax
Forked from laziel/unlock.js
Created March 3, 2017 15:41
Show Gist options
  • Save werelax/a5609a48a15403c1e06994bff44f41b5 to your computer and use it in GitHub Desktop.
Save werelax/a5609a48a15403c1e06994bff44f41b5 to your computer and use it in GitHub Desktop.
Unlock Web Audio in iOS 9 Safari
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