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
What are the new multimedia elements in HTML5?
How do you insert a video in HTML?
What is the difference between HTML and HTML5?
What is an iframe in HTML and what is it used for?
What is the difference between canvas and SVG in HTML5?
What is the difference between localStorage, sessionStorage and cookies?