HTML5 Audio

The <audio> tag plays sound, like music or a podcast, directly in the browser. Add the controls attribute to show the player buttons.


Basic example

<audio controls>
  <source src="song.mp3" type="audio/mpeg">
  <source src="song.ogg" type="audio/ogg">
  Your browser does not support the audio tag.
</audio>

The browser picks the first file format that it supports. MP3 works in all modern browsers, so Ogg is only an extra choice.


Attributes of the audio tag

AttributeMeaning
controlsShow the player with play, pause and volume
autoplayStart automatically. Most browsers block this for sound
loopPlay again and again
mutedStart with the sound off
preloadHow much to load first: none, metadata or auto

Supported formats

FormatType value
MP3audio/mpeg
WAVaudio/wav
Oggaudio/ogg
Note: Do not play sound automatically when a page opens. It annoys visitors, and browsers stop it anyway until the visitor clicks on the page.

Summary


Congratulations! Chapter Finished. Learn more about the similar topics:
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.