The <audio> element in HTML is used to embed sound files such as music, voice recordings, or sound effects into a webpage. It supports common formats like MP3, WAV, and OGG.
<audio src="audio-file.mp3" controls></audio>
| Format | Description |
|---|---|
| MP3 | Most widely supported format |
| WAV | High-quality uncompressed |
| OGG | Open-source compressed |
Different browsers support different audio formats. Using <source> ensures compatibility:
<audio controls>
<source src="sound.mp3" type="audio/mpeg">
<source src="sound.ogg" type="audio/ogg">
Your browser does not support the audio element.
</audio>
| Attribute | Description |
|---|---|
| controls | Displays play/pause, seek bar, and volume controls |
| autoplay | Starts playing automatically |
| loop | Repeats the audio continuously |
| muted | Starts audio with the sound turned off |
| preload | Controls how the audio loads (auto, metadata, none) |
| Value | Meaning |
|---|---|
| auto | Browser decides; often loads entire file |
| metadata | Only loads basic info (duration, size) |
| none | Does not load the file until the user plays it |
<audio
src="music.mp3"
controls
autoplay
loop
muted
preload="metadata">
</audio>
<audio controls>
<source src="song.mp3" type="audio/mpeg">
<source src="song.ogg" type="audio/ogg">
Your browser does not support the audio element.
<a href="song.mp3">Download the audio</a>
</audio>