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 OverflowAs @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 })
})
});
In order to be allowed to autoplay, either the user needs to have interacted with the page or the audio/video needs to be muted.
You can try:
let audio = new Audio('a_file');
audio.muted = true;
audio.addEventListener("canplaythrough", () => {
audio.play()
});
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.
javascript - Is it possible to play audio without the user interact with the web before? how would I do that? - Stack Overflow
html - How to play play sound without user's intervention using JavaScript or jQuery? - Stack Overflow
javascript - User interaction needed to play audio workaround? - Stack Overflow
[feat] Play Audio without needing user interaction
You can do it without html markup.
But keep in mind that audio file need to be load before the user minimize the screen. And the user have to interact (scroll, click, or any touch or click event) with document before minimize (as any autoplay feature).
Or you gonna have following error: Uncaught (in promise) DOMException: play() failed because the user didn't interact with the document first
Take a look to google about autoplay: https://developers.google.com/web/updates/2017/09/autoplay-policy-changes
So if you try something like that it's will not work:
$(element).on('your-event', function(){
var audio = new Audio('https://file-examples-com.github.io/uploads/2017/11/file_example_MP3_700KB.mp3');
audio.play();
}
But this way should work
var audio = new Audio('https://file-examples-com.github.io/uploads/2017/11/file_example_MP3_700KB.mp3');
$(element).on('your-event', function(){
audio.play();
}
Here is a working example on codepen: https://codepen.io/zecka/pen/VwjxGbE
well you can user Howl library to play the sound with user interaction I will share you my code
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
HOWL CDN
<script src="https://cdnjs.cloudflare.com/ajax/libs/howler/2.1.2/howler.core.min.js">
setTimeout(() => {
var sound = new Howl({
src: ['{{ asset('assets/mp3/notification.mp3') }}']
});
sound.play();
}, 600);