Playing a song at the end of countdown

I have a countdown timer for someone’s birthday. It displays the number of days, hours etc till the birthday, and on or after the birthday it says Happy Birthday and should play a song. The countdown works fine but the song doesn’t play, giving the following console error:

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

I understand the issue - I hate sites that automatically play vids or music - but is there a way to get the song to play, without saying something like “click here!”?

I’ve put my code here - and yes I realise the song won’t play on Codepen 'cos the mp3’s missing.


“I hate X, but is there a way to do X”… :scratch-head:

A splash screen is the ‘clean’ way to do it. Preferably one that notifies that audio may be played.

If there was a workaround for this, every advertiser in the world would be doing it.

Point taken but this isn’t a public page.

I’ve come across a few news websites that seem to start videos without user interaction.

If you’re running it as a file on your local machine, I think you can set the browser to allow it to happen.

If it’s being served by a remote webserver, your browser has no concept of a ‘public page’.

Are they Javascript videos, or is it an applet/player that starts that way?

Hmm. I’ve not looked TBH.

I’ve decided to add a click me button when the countdown ends…

How about using a click button to start the countdown?

1 Like