As @Unmitigated mentioned in the comment, user interaction is mandatory to play an audio. If you want to play the user interaction, modify the code as below

audio.addEventListener("canplaythrough", () => {
   audio.play().catch(e => {
      window.addEventListener('click', () => {
         audio.play()
      })
   })
});

Edited Add { once: true } to trigger only once.

audio.addEventListener("canplaythrough", () => {
   audio.play().catch(e => {
      window.addEventListener('click', () => {
         audio.play()
      }, { once: true })
   })
});
Answer from Pranavan on Stack Overflow
🌐
Reddit
reddit.com › r/webdev › play an audio file without user interaction
r/webdev on Reddit: Play an Audio File WITHOUT User Interaction
August 23, 2023 -

Hello,

Im writing a wordpress plugin that monitors woocommerces shop_orders page. When a new order comes in the page is supposed to play a tada sound.

Everything is hooked into the shop_orders page in the admin. The idea is the user navigates to that page, every 30 seconds the page refreshes, during the refresh a filter is triggered and we check for the presence of a new order. This all works perfectly.

Now, when the new order is found, during this load we also output some javascript to play the notification sound:

if (($sounds) && (self::$new_order)) {

$plugin_dir = plugin_dir_url("mgd-woorestaurants-orderpickups/assets/") . "assets/tada.mp3";

echo "<script type='text/javascript'>let notifi= new Audio('$plugin_dir'); notifi.play();</script>";

}

I see the code in the page source, but no sound is every played. The console shows an error that reads:

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

I've read about the chrome policies for audio and video, and I cant seem to work out a way to get this audio file to play when a new order is found.

How do I set the browser to ignore its policies for this specific purpose?

Thanks in advance.

Discussions

javascript - Is it possible to play audio without the user interact with the web before? how would I do that? - Stack Overflow
I need my webpage to play an audio clip without the user interact with the page, I know is poor UX and shouldn't be done that way, but that's what my clients are asking for. I also know browsers ... More on stackoverflow.com
🌐 stackoverflow.com
May 28, 2020
html - How to play play sound without user's intervention using JavaScript or jQuery? - Stack Overflow
I am working on the Asp.Net MVC Web application. I am calling one javascript method using SignalR, and in this method, I am trying to play sound from some backend event, but it doesn't play if I am... More on stackoverflow.com
🌐 stackoverflow.com
javascript - User interaction needed to play audio workaround? - Stack Overflow
Currently I am working on a html5/js music player application. I have many users on mobile devices that have problems with playing the music. According to many websites you need to have user intera... More on stackoverflow.com
🌐 stackoverflow.com
[feat] Play Audio without needing user interaction
There's a permission error when playing audio without user interaction. This audio permission should not apply to Tauri apps. ... Apps should be able to play sound without needing user interaction. More on github.com
🌐 github.com
28
February 16, 2022
🌐
Sololearn
sololearn.com › en › Discuss › 1914195 › solved-how-to-play-audio-in-javascript-without-any-action-by-user
[SOLVED] How to play audio in Javascript without any action by user. | Sololearn: Learn to code for FREE!
August 2, 2019 - Coder that's what!! Like in old games just make a demo prompt to ask user play sound or not On click yes of the prompt Play all the sounds you want to play In zero volume and loop = true..
Find elsewhere
🌐
YouTube
youtube.com › watch
Automatically Play Sound File in JavaScript Without User Interaction - YouTube
Learn how to automatically play a sound file in JavaScript without requiring any user interaction. Discover practical tips and examples with this comprehensi...
Published   January 27, 2025
Views   61
🌐
GitHub
github.com › tauri-apps › tauri › issues › 3478
[feat] Play Audio without needing user interaction · Issue #3478 · tauri-apps/tauri
February 16, 2022 - Describe the problem There's a permission error when playing audio without user interaction. This audio permission should not apply to Tauri apps. new Audio('audio.mp3').play(); Unhandled Promise Rejection: NotAllowedError: The request i...
Author   lynxionxs
🌐
PlayCanvas
forum.playcanvas.com › help & support
How to play audio autoplay without user interaction in - Help & Support - PlayCanvas Discussion
August 13, 2022 - hie everyone, sound starts only when someone make first tap or user interaction in game I know browsers policies changed but I found one post that block browser policies and play sound automatically can anyone help how to integrate in playcanvas here is the post link
🌐
Stack Overflow
stackoverflow.com › questions › 79247037 › video-or-audio-autoplay-play-with-sound-without-user-interaction
javascript - Video or Audio autoplay play with sound without user interaction - Stack Overflow
October 22, 2024 - Browser policy prohibits autoplay to prevent abuse. At least minimal user interaction with the page must occur for the sound to play.
🌐
Stack Overflow
stackoverflow.com › questions › 71252191 › how-to-play-background-music-in-javascript-without-user-interaction
html - How to play background music in JavaScript without user interaction? - Stack Overflow
The OP mentions he auto-starts these in the morning, now the only issue here this prevents any user interaction, so obviously the sound policy kicks in. So another idea, using something like puppeteer you could start the app, place a button on your website, that could play a startup sound, or even a silent mp3.
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
How to play MP3 in the background music automatically? - JavaScript - The freeCodeCamp Forum
August 23, 2019 - How to Play an MP3 in the Background Automatically [Solved] If you’re building a web page and have ever wondered how to automatically play music, this article is for you. However, while there are a few legitimate reason…
🌐
PlayCanvas
forum.playcanvas.com › help & support
[SOLVED] Audio will not play without a user interaction - Help & Support - PlayCanvas Discussion
October 8, 2019 - Ok so instead of autoplaying when the game starts,the audio starts when the user press the game instead?? Is this a bug or was this what this function was made to do?
🌐
W3Schools
w3schools.com › jsref › met_audio_play.asp
HTML DOM Audio play() Method
Tip: Use the controls property to display audio controls (like play, pause, seeking, volume, etc, attached on the audio). ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com · If you want to report an error, or if you want to make a suggestion, send us an e-mail: help@w3schools.com · HTML Tutorial CSS Tutorial JavaScript Tutorial How To Tutorial SQL Tutorial Python Tutorial W3.CSS Tutorial Bootstrap Tutorial PHP Tutorial Java Tutorial C++ Tutorial jQuery Tutorial