HTML5 Audio
HTML
What is the <audio>
tag?
The <audio>
element lets you embed sound content (like music or sound effects) directly into your web page, without needing extra plugins (like Flash).
It is similar to the <video>
tag, but for audio only.
This will display a simple audio player with play/pause, volume, and scrub controls.
Attributes
Attributes | Description |
---|---|
src | Path to the audio file. |
controls | Displays the built-in audio controls. |
autoplay | Starts playing automatically when the page loads. |
loop | Repeats the audio once it ends. |
muted | Loads the audio muted by default. |
Multiple Sources
The <source>
elements let the browser pick the best format it supports.
The fallback text is displayed if the browser doesn't support <audio>
.
Why use multiple <source>
?
Different browsers support different audio formats. This ensures your audio works across all major browsers.
Format | File Extension | Browser Support |
---|---|---|
MP3 | mp3 | Almost all modern browsers |
Ogg | ogg | Good open format; works in Firefox & Chrome |
WAV | .wav | Large file size but widely supported |
Example with autoplay, loop, and muted
- Start automatically
- Loop forever
- Play muted by default