简体   繁体   中英

Play audio when it is loaded in HTML5 and JavaScript

I have a simple HTML5 audio tag on my page. How can I automatically play audio file, when it just will be loaded? Thank you in advance.

You should set the autoplay attribute to "autoplay":

<audio autoplay="autoplay">
  <source src="song.ogg" type="audio/ogg" />
</audio>

But since autoplay is a boolean attribute you can just set it

<audio autoplay>
  <source src="song.ogg" type="audio/ogg" />
</audio>

Simple way is to put "autoplay" attribute into your "audio" tag. Or you can use some .play() function on the element when the DOM will be ready (or the audio will be preloaded).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM