To play an audio file using HTML, utilize the <audio> element:
<audio controls> <source src=”horse.ogg” type=”audio/ogg”> <source src=”horse.mp3″ type=”audio/mpeg”> Your browser does not support the audio element. </audio> |
The controls attribute adds audio controls such as play, pause, and volume.
The <source> element allows specification of alternative audio files that browsers can choose from, with the first recognized format being used.
Text between the <audio> and </audio> tags appears only in browsers that do not support the <audio> element.
To automatically start playing an audio file, use the autoplay attribute:
<audio controls autoplay> <source src=”horse.ogg” type=”audio/ogg”> <source src=”horse.mp3″ type=”audio/mpeg”> Your browser does not support the audio element. </audio> |
Note that in Chromium browsers, autoplay is generally restricted, but muted autoplay is consistently permitted. |
Include the muted attribute after autoplay to enable your audio file to start playing automatically (but muted):
<audio controls autoplay muted> <source src=”horse.ogg” type=”audio/ogg”> <source src=”horse.mp3″ type=”audio/mpeg”> Your browser does not support the audio element. </audio> |
The numbers in the table indicate the earliest browser version that fully supports the <audio> element.
The three supported audio formats are MP3, WAV, and OGG. Browser support varies for each of these formats.
Browser |
MP3 |
WAV |
OGG |
Edge/IE |
YES |
YES* |
YES* |
Chrome |
YES |
YES |
YES |
Firefox |
YES |
YES |
YES |
Safari |
YES |
YES |
NO |
Opera |
YES |
YES |
YES |
*From Edge 79
File Format |
Media Type |
MP3 |
audio/mpeg |
OGG |
audio/ogg |
WAV |
audio/wav |
The HTML DOM provides methods, properties, and events specifically for the <audio>
element.
These enable tasks such as loading, playing, and pausing audio, as well as adjusting settings like duration and volume. Additionally,
DOM events are available to notify you when an audio starts playing, is paused, and more.
Tag |
Description |
<audio> |
Defines sound content |
<source> |
Defines multiple media resources for media elements, such as <video> and <audio> |