D
D
Dima Kl2018-11-29 15:20:09
JavaScript
Dima Kl, 2018-11-29 15:20:09

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();
};

Actually this "garden" does not give the desired result.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question