
Ask HN: What is the best way to start  autoplay? - maxraz
As you know, some browsers prevent &#x27;autoplay&#x27; in &lt;audio id=&quot;sound&quot; src=&quot;track.mp3&quot; autoplay&gt;&lt;&#x2F;audio&gt;. I have 2 solutions and need to choose only one:<p>1) &lt;body onclick=&quot;document.getElementById(&#x27;sound&#x27;).play();&quot;&gt;<p>VS<p>2) 
document.addEventListener(&#x27;click&#x27;, musicPlay); 
function musicPlay() { 
  document.getElementById(&#x27;sound&#x27;).play(); 
}<p>Thank you.
======
mtmail
Both do the same. (2) is more modern syntax as onclick DOM attributes are
deprecated.

[https://stackoverflow.com/](https://stackoverflow.com/) might be better
suited for specific programming language questions.

~~~
maxraz
Thank you, Sir.

