Answer the question
In order to leave comments, you need to log in
How to bypass safari limit on autoplay?
Good day.
I ran into such a problem and I don’t know how to solve it (2 days I struggle, I tried all sorts of methods ... nothing helps ...)
There is a certain page that displays some information ... and under certain conditions, when the user loads the page sound should play automatically. It works in all browsers except safari... Yes, I am aware that there is a limit set in the browser itself. Actually, the question is: is it possible to get around this limitation using pure javascritp ?
The site is technical, for one specific client.
<audio id="audio" preload loop muted controls autoplay>
<source src="sound.mp3" type="audio/mpeg">
</audio>
async function play() {
try {
await audio.play();
btn.className = "playing";
} catch(err) {
btn.className = "";
}
}
function handlebtn() {
if (audio.paused) {
play();
} else {
audio.pause();
btn.className = "";
}
}
window.onload = function(){
audio.muted = false;
play();
event = new Event("click");
btn.addEventListener("click", handlebtn, false);
btn.dispatchEvent(event);
play();
};
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question