I create websocket server in python to handle notification event. Now, i can receive notification, the problem is i can't play sound because new autoplay policy changed, if i play sound using javascript it give me domexception. Any suggestion please ?
-
3Suggestion - Don't fight the trend against autoplay – charlietfl Oct 06 '19 at 12:02
-
2The policy is there for really good reasons. Ask the user if he want have sound and everything will be fine. Every game act like that. It doesnt start yelling immediatly. – Thomas Ludewig Oct 06 '19 at 12:04
-
Can you add some code, please? – ISD Oct 06 '19 at 12:11
-
probably related [Why is Stack Overflow trying to start audio?](https://meta.stackexchange.com/q/331960/165773) <--- @charlietfl trend appears to be opposite over here :( – gnat Oct 06 '19 at 13:27
-
@gnat Firefox for example has now gone to default of no autoplay unless user manages settings for it https://hacks.mozilla.org/2019/02/firefox-66-to-block-automatically-playing-audible-video-and-audio/ – charlietfl Oct 06 '19 at 15:39
2 Answers
As i know, playing sound is simple in html-javascript. like this example: https://stackoverflow.com/a/18628124/7514010
but it depend to your browsers and how you load and play, so issues is:
- Some of browsers wait till user click something, then let you play it (Find a way for it)
- In some case browsers never let you play till the address use SSL (means the HTTPS behind your url)
- The loading be late so the playing be late / or even not start.
So i usually do this:
HTML
<audio id="notifysound" src="notify.mp3" autobuffer preload="auto" style="visibility:hidden;width:0px;height:0px;z-index:-1;"></audio>
JAVASCRIPT (Generally)
var theSound = document.getElementById("notifysound");
theSound.play();
And the most safe if i want sure it be played when i notify is :
JAVASCRIPT (In your case)
function notifyme(theTitle,theBody) {
theTitle=theTitle || 'Title';
theBody=theBody || "Hi. \nIt is notification!";
var theSound = document.getElementById("notifysound");
if ("Notification" in window && Notification) {
if (window.Notification.permission !== "granted") {
window.Notification.requestPermission().then((result) => {
if (result != 'denied') {
return notifyme(theTitle,theBody);
} else {
theSound.play();
}
});
} else {
theSound.play();
try {
var notification = new Notification(theTitle, {
icon: 'icon.png',
body: theBody
});
notification.onclick = function () {
window.focus();
};
}
catch(err) {
return;
}
}
} else {
theSound.play();
}
}
(and just hope it be played. because even possible to volume or some customization make it failed.)

- 915
- 10
- 15
-
yes it work, but why if i refresh the page the sound won't play. It give me uncaught (in promise) domexception. It will work again if i logout and login again. Thanks for your response btw – Uis Yudha Oct 07 '19 at 06:08
-
have to share your code with good explanation, for checking your code. + this code will work well just when browser is open, there is **no any service worker** in your question. so i think you should change your question to how you can play sound by a service worker notification. and that the answer is there is an option for that in android (https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages?authuser=0#ApnsConfig). and if you talk about just browser, from service workers, there is some same questions like : https://stackoverflow.com/q/42268644/7514010 – MMMahdy-PAPION Oct 07 '19 at 10:19
-
to bypass new autoplay policy :
create a button that can play the sound, hide it and trigger the sound with :
var event = new Event('click');
playBtn.dispatchEvent(event);
EDIT
assuming you have :
let audioData = 'data:audio/wav;base64,..ᴅᴀᴛᴀ...'; // or the src path
you can use this function to trigger whenever you want without appending or create element to the DOM:
function playSound() {
let audioEl = document.createElement('audio');
audioEl.src = audioData;
let audioBtn = document.createElement('button');
audioBtn.addEventListener('click', () => audioEl.play(), false);
let event = new Event('click');
audioBtn.dispatchEvent(event);
}
usage :
just playSound()
EDIT 2
I re test my code and it does'nt work hum ... weird

- 2,271
- 1
- 8
- 16
-
i've tested it and it work, but why if i refresh the page the sound won't play. It give me uncaught (in promise) domexception. It will work again if i logout and login again. Thanks for your suggestion – Uis Yudha Oct 07 '19 at 06:09