Search code examples
javascriptaudioprompt

Prompt user for sound permission


Is this even a thing?

Okay I have a quick intro, the intro shows a logo and plays a sound according to the animation, is unskippable and doesn't take more than 4 seconds

The problem is, if the users don't give the permission the audio will be handled automatically and this will prevent to actually play the sound or delay it if the user don't interact with it or does it late in future visits

This seems to happen on Chrome

How can I promt the user about sound permission needs to be handled by him, asking to grant permission if he wants?

Or is there a way to keep playing the sound even if the sound is in "auto", meaning haven't been interacted with by the user yet?

Or how I can anyway play the sound silently on background and, when the user interacts with it, raise the volume so the sound sounds perfectly timed and not delayed?

Or what is the best approach i must take that will be much appreciated?

I've already tried a bunch of things that doesn't seem to effect a ****, like using Howler audio library (amazing by the way) searching for an unexisting answer and many other things in vain


Solution

  • To anyone who ever will be stuck on here check this:

    https://developers.google.com/web/updates/2017/09/autoplay-policy-changes

    I was able to check the AudioContext State and then if suspended, set an interval counting each millisecond, if the user, meanwhile the sound "that must be playing" interacts with the page, this sound will be resumed on that lovely position mantaining the freaking desired and badly needed timing

    Then a button suggest the user to go ahead and feel touchy

    Using howler you can check the AudioContext State easily with

    Howler.ctx.state