Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save pavle-goloskokovic/70534713b9d42d2bec27df0d11975f58 to your computer and use it in GitHub Desktop.
Save pavle-goloskokovic/70534713b9d42d2bec27df0d11975f58 to your computer and use it in GitHub Desktop.
When resume method promise resolves we are certain that audio context has been unlocked and that we can remove touch event listeners
if (context.state === 'suspended' && 'ontouchstart' in window)
{
var unlock = function()
{
context.resume().then(function()
{
document.body.removeEventListener('touchstart', unlock);
document.body.removeEventListener('touchend', unlock);
});
};
document.body.addEventListener('touchstart', unlock, false);
document.body.addEventListener('touchend', unlock, false);
}
@pavle-goloskokovic
Copy link
Author

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment