Skip to content

Instantly share code, notes, and snippets.

@pavle-goloskokovic
Created January 20, 2018 12:31
Show Gist options
  • Save pavle-goloskokovic/c0fb2e2992127fa5c3ef42574f93a0f6 to your computer and use it in GitHub Desktop.
Save pavle-goloskokovic/c0fb2e2992127fa5c3ef42574f93a0f6 to your computer and use it in GitHub Desktop.
In order to try and unlock audio context as soon as possible we will run our code on the first 'touchstart' event that occurs anywhere on the page
if (context.state === 'suspended' && 'ontouchstart' in window)
{
var unlock = function()
{
context.resume();
};
document.body.addEventListener('touchstart', 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