Last active
June 13, 2018 22:18
-
-
Save pavle-goloskokovic/141cd38b82d719878b6686b07ca56ad6 to your computer and use it in GitHub Desktop.
Wrap everything inside of a Promise and returning it from our function
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
function webAudioTouchUnlock (context) | |
{ | |
return new Promise(function (resolve, reject) | |
{ | |
if (context.state === 'suspended' && 'ontouchstart' in window) | |
{ | |
var unlock = function() | |
{ | |
context.resume().then(function() | |
{ | |
document.body.removeEventListener('touchstart', unlock); | |
document.body.removeEventListener('touchend', unlock); | |
resolve(true); | |
}, | |
function (reason) | |
{ | |
reject(reason); | |
}); | |
}; | |
document.body.addEventListener('touchstart', unlock, false); | |
document.body.addEventListener('touchend', unlock, false); | |
} | |
else | |
{ | |
resolve(false); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I’ve created an npm package called web-audio-touch-unlock
that exposes this method and makes it easy to use in your own projects.
Just pass your AudioContext instance to it and you’re good to go!
You can try it out yourself here.
Cheers!