D Dev Notebook

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 plugins like Flash. It works similarly to the <video> tag, but is used for audio only.

Basic Example

<audio src="song.mp3" controls>
Your browser does not support the audio element.
</audio>

This creates an audio player with play/pause, volume, and progress controls:

Important Attributes

AttributeDescription
srcPath to the audio file.
controlsDisplays the built-in audio controls.
autoplayStarts playing automatically when the page loads.
loopRepeats the audio once it ends.
mutedLoads the audio muted by default.

Using Multiple Sources

You can provide multiple formats for better browser compatibility:

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

Why use multiple <source>?

Different browsers support different audio formats. This ensures your audio works across all major browsers.

FormatFile ExtensionBrowser Support
MP3.mp3Almost all modern browsers
Ogg.oggGood open format; works in Firefox & Chrome
WAV.wavLarge file size but widely supported

Example with autoplay, loop, and muted

<audio controls autoplay loop muted>
<source src="background.mp3" type="audio/mpeg">
<source src="background.ogg" type="audio/ogg">
Your browser does not support the audio element.
</audio>
  • Starts automatically
  • Loops forever
  • Plays muted by default