#Playing mp3 sound on page load

4 messages · Page 1 of 1 (latest)

spring plank
#

Hey folks, the issue is that I want to play the audio sound on page load. But it plays only after I click on some button. (i am using nextjs 12)
In _app.js:

useEffect(() => {
    const birds = new Audio(soundurl);
    birds.setAttribute("muted", true);
    birds.play();
  });   //no deps array is provided

Now, this also doesnt play when i refresh the page.
Can anyone please help with this?

cerulean forge
#

This isn’t Next related. Browsers won’t auto play audio without a user action.

slender frost
#

Please do not play any audio or video without the user explicitly clicking on some buttons. That is very bad user experience.

left halo
#

Not allowed by 99% of browsers and that’s a good thing