D DevBrainBox

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.

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

This will display a simple audio player with play/pause, volume, and scrub controls.

Attributes

AttributesDescription
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.

Multiple Sources

<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>

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.

FormatFile ExtensionBrowser Support
MP3mp3Almost all modern browsers
OggoggGood 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>
  • Start automatically
  • Loop forever
  • Play muted by default

On this page