Alex Pliutau
Alex Pliutau

Reputation: 21937

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.

Upvotes: 0

Views: 413

Answers (2)

YuS
YuS

Reputation: 2045

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).

Upvotes: 2

Nicola Peluchetti
Nicola Peluchetti

Reputation: 76910

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>

Upvotes: 4

Related Questions