Thuta Learning
HTML
AdvancedWeb DevelopmentBeginner

Audio

HTMLLesson 3025 min

What you'll walk away with

  • Predict what an <audio> element looks like with and without controls
  • Order MP3, Ogg and M4A sources by preference with correct type values
  • Explain why pages with many episodes use preload="none"
  • Add a transcript and a download link for audio-only content

Explain it simply

Explain it simply

<audio> is like a small radio built into your page. Point it at a sound file and the browser draws one row with a play button, the time, a progress bar and the volume.

Unlike video, there is no picture, so the controls are the entire visible part of an <audio> element. Remove the controls attribute and the radio is still in the page, but it disappears completely. When you offer several sources, the browser chooses a format it can play by itself.

Problem → Why → Solution

The problem: background music nobody can stop

A coffee shop website wants every visitor to feel the shop's mood, so it adds the single line below. Because there are no controls, nothing is visible on the page.

For most visitors nothing happens, because the browser blocks autoplay with sound. For someone who visits the site often, the browser may allow it, and the music suddenly starts with no button to stop it. A screen reader user hears the page reading drowned under the song, and someone in an office closes the tab fast and never comes back.

index.html
<!-- Invisible (no controls), may start with sound, cannot be paused -->
<audio src="cafe-music.mp3" autoplay loop></audio>

Why this is a real problem

Browsers hide an <audio> without controls with display: none. WCAG also requires a way to pause, stop or change the volume of any audio that plays automatically for more than three seconds. Without it, the sound covers the screen reader's voice and the page becomes unusable.

Core concept: how <audio> works

<audio> shares its engine with <video>: the same source selection, the same autoplay policy and the same preload hint. The differences all come from having no picture: no poster, hardly any need for width/height, and no area where captions could be drawn. Four points matter.

1. controls is the only UI

With controls, Chrome shows a single player row about 300px wide. Without it, the element is invisible, and the only way to use it is to build your own buttons that call play() and pause() from JavaScript.

2. Source order is a preference list

The browser takes the first <source> it can play. Every modern browser plays MP3, so if MP3 is listed first the .ogg below it is never used. If you want the smaller Ogg Opus file to win, put it first and keep MP3 last as the safety net.

3. preload saves data

On a podcast page with twenty episodes, every player fetching metadata means twenty requests. preload="none" waits for the user to press play before downloading, which protects mobile data, at the cost of not showing the duration until then.

4. The text alternative lives in the page

An <audio> element has no picture area, so browsers do not display <track> text for it. Deaf and hard-of-hearing visitors need a transcript in the page or behind a link, and the transcript also lets search engines find what the episode is about.

Formattype valueNotes
MP3audio/mpegPlays everywhere; the safety net
Ogg (Opus/Vorbis)audio/oggOften smaller at the same quality
AAC (.m4a)audio/mp4Common export from Apple apps
WAVaudio/wavUncompressed and very large
The life of one audio player
  1. controls make the player row appear
  2. Pick the first playable source
  3. Start when the user presses play
  4. Transcript covers the text alternative

If you cannot see the player, check controls first; if there is no sound, check the file path and the type in the Network tab.

General pattern

html
<audio controls preload="none|metadata|auto">
  <source src="preferred.ogg" type="audio/ogg">
  <source src="fallback.mp3" type="audio/mpeg">
  Fallback text with a download link
</audio>
<a href="transcript.html">Transcript</a>

Make the player visible with controls, list sources in order of preference, and keep a transcript link next to the player.

An audio player with two formats

The code below has controls and two sources. song.mp3 and song.ogg are placeholder names, so save your own audio files next to the HTML file under those names. As you read it, predict which file will actually play.

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

Code breakdown

  1. Visible player<audio controls>

    controls is what makes the player row appear. Remove it and the element becomes display: none and vanishes from the page.

  2. First choice: MP3<source src="song.mp3" type="audio/mpeg">

    The correct MIME type for MP3 is audio/mpeg. Every modern browser can play it, so this source is always the one chosen.

  3. Second choice: Ogg<source src="song.ogg" type="audio/ogg">

    It is used only by a browser that cannot play MP3, or when song.mp3 fails to load. To prefer Ogg, move this line above the MP3 one.

  4. Fallback textYour browser does not support the audio element.

    It appears only in very old browsers that do not know <audio>. Even if both files are missing, a modern browser shows a player that does nothing rather than this text.

You should see
A light grey player row about 300px wide with a play button, "0:00 / 0:00", a progress bar, volume and a ⋮ menu. With song.mp3 present, pressing play plays it; every modern browser can play MP3, so song.ogg is not used.

Marking up a complete podcast episode

In practice one player is not enough. <figure> and <figcaption> tie the episode title to the player, the download link serves people who listen offline, and the transcript link serves deaf visitors and anyone who prefers reading. The download attribute only works for same-origin files; for a file on another domain the browser just follows the link.

html
<figure>
  <figcaption>Episode 12: HTML forms that people finish (24 min)</figcaption>
  <audio controls preload="none">
    <source src="ep12.ogg" type="audio/ogg">
    <source src="ep12.mp3" type="audio/mpeg">
  </audio>
  <p>
    <a href="ep12.mp3" download>Download MP3</a> ·
    <a href="ep12-transcript.html">Read the transcript</a>
  </p>
</figure>

In real projects

In real projects

Podcast sites, language-learning apps and the "listen to this article" feature on news sites use <audio> every day. Language sites often put a short pronunciation clip next to each word, and instead of the full controls they wire a single "Play pronunciation" button to the <audio> element with JavaScript; that button then needs a clear label.

Podcast pages use preload="none" so a long episode list loads quickly, and they offer the same MP3 used in the RSS feed as a download link. To show audio hosted on Spotify or SoundCloud you use their <iframe> embed instead of <audio>. Whatever the route, teams that publish transcripts gain both accessibility and search traffic.

Common mistakes

Common mistakes

Forgetting controls

People write <audio src="lesson.mp3"></audio> and then spend half an hour in the CSS asking why the player is missing. The browser hides audio without controls; add controls or build your own button.

A type that does not match the file

Copy-pasting type="audio/ogg" onto an MP3 makes a browser without Ogg support skip a perfectly good file without downloading it. type must describe the real format; for MP3 it is audio/mpeg.

Listing MP3 first and shipping an Ogg nobody gets

With MP3 written first, every browser takes the MP3 and the effort of encoding the Ogg file is wasted. If you want Ogg used, list it first.

Publishing without a transcript

Adding a <track> feels like enough, but browsers do not display it for <audio>. Publish the transcript as HTML text.

Best practices

Best practices

  • Write the episode title and duration as text next to the player; with preload="none" the duration is not shown until play, so this text matters even more
  • Never start sound by itself; if you truly must, put a pause button where it is seen first
  • When you build your own play button, use a real <button> and a label that says what plays, such as "Play pronunciation of မင်္ဂလာပါ"
  • Speech usually stays clear at a low bitrate (for example around 64 kbps mono), which keeps files small; listen to your own export to confirm

Mini challenge

Two episodes for a language-learning page

Build a page for a "Myanmar for travellers" podcast. Each of the two episodes needs a title, a player, a download link and a transcript link, and opening the page must not download any audio.

  • Wrap each episode in <figure> with a <figcaption> (title + duration)
  • <audio> has controls and preload="none", with Ogg listed before MP3
  • Link the MP3 with the download attribute
  • Give every episode a transcript link
podcast.html
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Myanmar for travellers</title>
</head>
<body>
  <h1>Myanmar for travellers</h1>
  <!-- TODO: episode 1 "Greetings" (6 min): figure + figcaption -->
  <!--   audio: controls, preload="none", greetings.ogg then greetings.mp3 -->
  <!--   links: download greetings.mp3, transcript greetings.html -->

  <!-- TODO: episode 2 "Ordering food" (9 min), same structure -->
</body>
</html>

Expected: Two episodes under the h1, each with its title line, a player row about 300px wide and the two links "Download MP3 · Read the transcript". Before play is pressed, the duration still reads 0:00.

Show hint

Finish one episode completely, then copy it and change only the file names and the title. The type values are audio/ogg and audio/mpeg.

Show solution
podcast.html
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Myanmar for travellers</title>
</head>
<body>
  <h1>Myanmar for travellers</h1>

  <figure>
    <figcaption>Episode 1: Greetings (6 min)</figcaption>
    <audio controls preload="none">
      <source src="greetings.ogg" type="audio/ogg">
      <source src="greetings.mp3" type="audio/mpeg">
    </audio>
    <p>
      <a href="greetings.mp3" download>Download MP3</a> ·
      <a href="greetings.html">Read the transcript</a>
    </p>
  </figure>

  <figure>
    <figcaption>Episode 2: Ordering food (9 min)</figcaption>
    <audio controls preload="none">
      <source src="ordering-food.ogg" type="audio/ogg">
      <source src="ordering-food.mp3" type="audio/mpeg">
    </audio>
    <p>
      <a href="ordering-food.mp3" download>Download MP3</a> ·
      <a href="ordering-food.html">Read the transcript</a>
    </p>
  </figure>
</body>
</html>

Quick quiz 1 / 3

What do you see when <audio src="intro.mp3"></audio> (no controls) is in the page?

Quick quiz 2 / 3

song.mp3 (audio/mpeg) is listed before song.ogg (audio/ogg) and both files exist. Which file does Firefox play?

Quick quiz 3 / 3

What is the best way to make a podcast episode accessible to deaf visitors?

What to remember

  • controls is the whole visible part of <audio>; without it the element disappears
  • The browser takes the first playable source, so put your preferred format first
  • Make type match the real file format (MP3 = audio/mpeg)
  • preload="none" saves data on pages with many episodes
  • Give every audio-only recording a transcript and avoid autoplay background music

HTML Living Standard — Embedded Content — WHATWG

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.

Audio | Thuta Learning