Detect if JS has permission to play audio before attempting?

Detect if JS has permission to play audio before attempting?

adam

Mr. Webwide
Administrator
Local time
20:57
Joined
Sep 24, 2019
Messages
1,264
Pronouns
he/him

I have some JavaScript triggering HTML5 audio. My app saves preferences including audio playing so if the user returned I would like the audio to carry on playing. Now I understand that browsers require some interaction before playing this (at least, Chrome does).

Uncaught (in promise) DOMException: play() failed because the user didn't interact with the document first.

I am wondering if there is a way to detect this before it happens? Ideally, I would like a function that checks if there's permission to play audio (from first interaction or the browser doesn't care) and if so, play it, otherwise, show a play audio button which would trigger the function again.

Does anyone know if that's possible?

 
Last edited:

Gummibeer

Astroneer
Moderator
Local time
21:57
Joined
Oct 5, 2019
Messages
1,187
Pronouns
he/him

Don't use play but add an audio element with Autoplay. At least that's the workaround that worked some months ago.

We used the silence iframe in a game and it worked perfectly. 😊


Here is the source code:

 
Last edited:
Top