🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEhtml

html Documentation

LOADING ENGINE...

<audio>

AI & DATA SCIENCE // audio-tag

The <audio> element embeds sound content with native browser playback controls, supporting multiple source formats via nested <source> elements.

Syntax

<audio controls>
  <source src="song.mp3" type="audio/mpeg">
  <source src="song.ogg" type="audio/ogg">
  Your browser doesn't support audio.
</audio>

Deep Dive Course

**`<audio>`** works almost identically to `<video>`, minus the visual frame — it plays sound with the **`controls`** attribute showing native play/pause/volume/seek UI. Multiple `<source>` children let the browser choose a format it supports (MP3 has near-universal support today, making a single source often sufficient, though OGG/WAV are still used as fallbacks in some cases). It supports the same **`autoplay`**, **`loop`**, and **`muted`** attributes as `<video>`, with the same autoplay-requires-muted restriction.

1Understanding <audio>

`<audio>` works almost identically to <video>, minus the visual frame — it plays sound with the `controls` attribute showing native play/pause/volume/seek UI. Multiple <source> children let the browser choose a format it supports (MP3 has near-universal support today, making a single source often sufficient, though OGG/WAV are still used as fallbacks in some cases). It supports the same `autoplay`, `loop`, and `muted` attributes as <video>, with the same autoplay-requires-muted restriction.

💡

MP3 support is now nearly universal across browsers, so many modern sites only provide a single MP3 <source> rather than juggling multiple formats — check your actual target browser support before assuming you need fallbacks.

editor.html
<audio controls>
  <source src="/podcast-episode-1.mp3" type="audio/mpeg">
  Your browser doesn't support the audio element.
</audio>
localhost:3000

2Practical Example

Here is a real-world application of <audio> showing how it is used in production HTML.

editor.html
<!-- Looping ambient background sound, muted by default -->
<audio autoplay loop muted id="ambience">
  <source src="/rain.mp3" type="audio/mpeg">
</audio>
<button onclick="document.getElementById('ambience').muted = false">Enable sound</button>
localhost:3000

3Best Practices

Follow these guidelines when working with <audio>:

1. Include controls so users have a visible way to play/pause/adjust volume

2. Provide fallback text content for browsers that don't support the element at all

3. Use loop for short sound effects or ambient audio that should repeat

⚠️

Tip: MP3 support is now nearly universal across browsers, so many modern sites only provide a single MP3 <source> rather than juggling multiple formats — check your actual target browser support before assuming you need fallbacks.

editor.html
<audio controls>
  <source src="/podcast-episode-1.mp3" type="audio/mpeg">
  Your browser doesn't support the audio element.
</audio>
localhost:3000

Examples

Example 01Basic Usage
<audio controls>
  <source src="/podcast-episode-1.mp3" type="audio/mpeg">
  Your browser doesn't support the audio element.
</audio>
Example 02Advanced Example
<!-- Looping ambient background sound, muted by default -->
<audio autoplay loop muted id="ambience">
  <source src="/rain.mp3" type="audio/mpeg">
</audio>
<button onclick="document.getElementById('ambience').muted = false">Enable sound</button>

Best Practices

  • Include controls so users have a visible way to play/pause/adjust volume
  • Provide fallback text content for browsers that don't support the element at all
  • Use loop for short sound effects or ambient audio that should repeat

Interview Question

Why might a site autoplay a muted <audio> element and then let the user unmute it with a button, rather than just autoplaying with sound?

Hint: Think about browser autoplay policies and user experience expectations.

Browsers block autoplay-with-sound outright, so starting muted and then unmuting via an explicit user click (a genuine user gesture) is the only reliable way to have audio 'ready to go' immediately while still respecting both the browser's autoplay policy and the user's expectation of not being surprised by sudden sound — the user opts in with a deliberate click rather than having audio forced on them.

Exercises

EasyPractice using <audio> in a real scenario.
View Solution
<audio controls>
  <source src="/podcast-episode-1.mp3" type="audio/mpeg">
  Your browser doesn't support the audio element.
</audio>

Frequently Asked Questions

Why might a site autoplay a muted <audio> element and then let the user unmute it with a button, rather than just autoplaying with sound?

Browsers block autoplay-with-sound outright, so starting muted and then unmuting via an explicit user click (a genuine user gesture) is the only reliable way to have audio 'ready to go' immediately while still respecting both the browser's autoplay policy and the user's expectation of not being surprised by sudden sound — the user opts in with a deliberate click rather than having audio forced on them.

Related Functions

video-tagcontrolsloopmuted