Lesson 45 of 51 · Module 7, lesson 5 of 11 · 3 min

Audio

Add sound with a player, a fallback format and a transcript.

What you'll learn

  • How to add sound with <audio>
  • How to offer a second format as a fallback
  • Why every audio clip needs a transcript

Why this matters

A recorded tip, a podcast, the pronunciation of a word: audio is useful. But a deaf visitor, or someone in a quiet library, gets nothing from it — unless you also give the words as text.

An audio player

Save soup-tip.mp3 into your media folder (right-click → Save link as…).

html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Cooking tip</title>
</head>
<body>
  <h1>Today's cooking tip</h1>
  <audio controls>
    <source src="media/soup-tip.mp3" type="audio/mpeg">
    <source src="media/soup-tip.ogg" type="audio/ogg">
    <p>Your browser cannot play audio. <a href="media/soup-tip.mp3">Download the tip</a>.</p>
  </audio>
  <h2>Transcript</h2>
  <p>Here is a cooking tip. Add a little salt at the end, then taste the soup before you serve it.</p>
</body>
</html>

What you'll see: a small player bar with a play button and a time line, and the transcript under it.

The page "Today's cooking tip" with an audio player bar showing a play button, the time and a volume control, followed by the heading "Transcript" and the text of the tip.

  • controls shows the player. Without it, the audio is invisible.
  • Two <source> elements: the browser plays the first format it supports. MP3 plays in every current browser; the OGG file is an optional extra.
  • type="audio/mpeg" is the type for MP3 files.

Always add a transcript

A transcript is the text of everything that is said. Put it right under the player, as in the example. It is the audio version of alt text. It also lets people find a sentence quickly, copy it, or translate it.

No autoplay

autoplay on audio is blocked by most browsers unless the audio is muted — and sound that starts by itself is a real problem for screen reader users, whose screen reader is suddenly drowned out. Let people press play.

Try it yourself

  1. Select the example and choose Try it, and play the tip.
  2. Remove controls. The player disappears from the preview. Put it back.
  3. Record a 10-second tip of your own on your phone, save it as MP3 in media, and add it with a transcript.

Common mistakes

  • No controls. Nothing appears on the page. Fix: add controls.
  • No transcript. Deaf and hard-of-hearing visitors miss everything. Fix: add the text under the player.
  • Wrong type, such as audio/mp3. Some browsers skip the source. Fix: audio/mpeg for MP3.

Check yourself

  1. What does the second <source> do?
  2. What is the audio version of alt text?
  3. What is the correct type for an MP3 file?

Answers

  1. It is a fallback: the browser plays the first format it supports.
  2. A transcript.
  3. audio/mpeg.

Next: sometimes you want to show a whole page from somewhere else inside yours — a map, or a YouTube video. That is an iframe.

Chat with us on WhatsApp