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 background music in JavaScript without user interaction? - Stack Overflow
The user does not necessarily interact with the site because it starts up automatically, hence the problem when there's an incoming call. ... Is your website a single page application?. (SPA) ... Not sure if the OP's website is a SPA, if your using React, and using something like React-Router etc, this is a SPA (Single Page Application). Playing Audio ... More on stackoverflow.com
🌐 stackoverflow.com
How to play audio autoplay without user interaction in - Help & Support - PlayCanvas Discussion
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 More on forum.playcanvas.com
🌐 forum.playcanvas.com
0
August 13, 2022
Audio Without User Interaction?
Any way to start audio without having a user click a key or button for p5.js? More on discourse.processing.org
🌐 discourse.processing.org
0
0
February 2, 2021
🌐
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 - Just ask the user "play sound" With yes no button And made useres to click on a "yes" button Then on click of the button play all the sounds you have in zero volume and in loop mode *important* Then whenever you want the sound to play Set ...
🌐
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.
🌐
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
Find elsewhere
🌐
Medium
medium.com › @harryespant › bypassing-browser-autoplay-restrictions-a-smart-approach-to-notification-sounds-9e14ca34e5c5
Bypassing Browser Autoplay Restrictions: A Smart Approach to Notification Sounds | by Harry Es Pant | Medium
July 30, 2025 - Unlock on first interaction — Use the user gesture to “unlock” audio permissions · Cache the unlocked state — Remember that audio is now allowed to play · Here’s a complete composable that implements this approach: import { ref } ...
🌐
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
🌐
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…
🌐
Mozilla
developer.mozilla.org › en-US › docs › Web › Media › Autoplay_guide
Autoplay guide for media and Web Audio APIs - Media | MDN
December 20, 2024 - Media with an active audio track are considered to be audible, and autoplay blocking applies to them. Inaudible media are not affected by autoplay blocking. The term autoplay refers to any feature that causes media to begin to play without the user specifically requesting that playback begin.
🌐
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
🌐
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?
🌐
Chrome Developers
developer.chrome.com › blog › htmlmediaelement.play() returns a promise
HTMLMediaElement.play() returns a promise | Blog | Chrome for Developers
March 11, 2016 - Various workarounds exist, but ... has now made it to the web platform, with an initial implementation in Chrome 50. A play() call on an a <video> or <audio> element now returns a Promise....