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
December 20, 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
Now the reason I asked the OP if his website was a SPA, these work well as Web App's, and Web Apps have a slightly different sound policy. aka, been able to play audio without user interaction. So how do you make your website into a Web App,.. Easy, you tell the browser :) For example in Chrome, ... More on stackoverflow.com
🌐 stackoverflow.com
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
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
September 20, 2022
🌐
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 ... 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....
🌐
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 audio.cu...
Find elsewhere
🌐
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
🌐
PlayCanvas
forum.playcanvas.com › help & support
How to play audio autoplay without user interaction in - Help & Support - PlayCanvas Discussion
September 20, 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
🌐
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
🌐
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
🌐
Chrome Developers
developer.chrome.com › blog › htmlmediaelement.play() returns a promise
HTMLMediaElement.play() returns a promise | Blog | Chrome for Developers
March 11, 2016 - var playPromise = document.querySelector('video').play(); // In browsers that don’t yet support this functionality, // playPromise won’t be defined. if (playPromise !== undefined) { playPromise.then(function() { // Automatic playback started! }).catch(function(error) { // Automatic playback ...
🌐
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 — ... approach: import { ref } from 'vue'; export function useNotificationSound() { let notificationSound = null; let isAudioReady = false; const showPermissionPrompt = ref(false); // Initialize ...
🌐
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…
🌐
Stack Overflow
stackoverflow.com › questions › 64433264 › how-can-i-download-and-play-audio-clips-without-user-interaction
javascript - How can I download and play audio clips without user interaction? - Stack Overflow
October 20, 2020 - Is there a way programmatically to begin audio play before the user has interacted with the page? With the following JavaScript: console.log('Playing abc.'); XYZ.abc = new Audio('audio/abc.wav'); X...
🌐
Stack Overflow
stackoverflow.com › questions › 60729586 › play-audio-without-user-interact-jquery
ajax - play audio without user interact jquery - Stack Overflow
March 18, 2020 - $(window).on( "click", function() { //prevent 'no interact error' setTimeout(function() { // start play after 5 sec var el = document.getElementById("mainaudio"); if(el.paused){ //check if music dont play now el.play(); } else{ console.log('already ...