0

I am designing an HTML page and added an audio in the header there using the below code:

<audio id="audio_play">
    <source src="voice/Story 2_A.m4a" type="audio/ogg" />
</audio>
<img class="head-icon" src="img/audio.png" onClick="document.getElementById('audio_play').play(); return false;" />
<img class="head-icon2" src="img/audio2.png" onClick="document.getElementById('audio_play').pause(); return false;" />

But now I want to autoplay the audio on page load. I am using autoplay both on the audio tag and the source tag. But can't help. Even removed the last 2 lines of the code following this, but that too won't help.

You can check the page directly from here.

As per the code added, it is playing the audio upon clicking on the icon now. I am not sure what I am missing to make it autoplay on page load.

  • you should read this https://developer.chrome.com/blog/autoplay/ – Himanshu Patil Nov 07 '21 at 09:13
  • Got it. Currently, clicking on last but one line code is playing the audio. Is there a script I can use to click the button automatically on page load? May be after a couple of seconds? – Jitendra Mishra Nov 07 '21 at 11:20
  • Does this answer your question? [How to make audio autoplay on chrome](https://stackoverflow.com/questions/50490304/how-to-make-audio-autoplay-on-chrome) – Himanshu Patil Nov 07 '21 at 13:44
  • This is not possible as it's blocked by default by browsers. This is for good as spammers can use it to spam users. – Himanshu Patil Nov 07 '21 at 13:48

1 Answers1

0

Audio/Video has been restricted from autoplaying. They can play only with user interaction. Also please keep the format of the audio file as ogg instead of m4a as you have specified the type as audio/ogg. Change from voice/Story 2_A.m4a to voice/something.ogg