CHAPTER 23 · HTML

Audio: add a player and keep the information available in words

Add a sound file with the browser's own controls. Understand sources, playback settings and fallback text, then provide a written alternative so the lesson does not depend on hearing the sound.

Download example files (ZIP)

The player and recording are different things

Think of a music player and a song file. The player supplies buttons; the file supplies the sound. HTML's audio element asks the browser to provide a player. Its src, or child source elements, identifies the recording.

The example ZIP contains media/practice-tones.wav: an original six-second sound with two quiet tones, one near the start and another around three seconds. There is no speech. Start at a comfortable device volume. The page does not play automatically.

Read the basic syntax

HTML fragment — part of a document
<audio controls src="media/practice-tones.wav">
  Your browser does not support this audio player.
</audio>

Audio has an opening and closing tag. Controls asks for the browser's built-in playback interface. This usually includes play, pause and seeking, but the exact controls and appearance depend on the browser and device. HTML does not promise the same-looking player everywhere.

The sentence inside is fallback content for a browser that does not support the element. A modern browser with a missing or unsupported audio file does not necessarily display that sentence instead. Provide a visible link and written equivalent outside the player too.

Complete program: audio-practice.html

audio-practice.html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Hear Two Practice Tones</title>
</head>
<body>
  <h1>Try the audio controls</h1>
  <p>Play a six-second example with two quiet tones. Nothing plays until you choose Play.</p>
  <audio controls preload="metadata" src="media/practice-tones.wav">
    Your browser does not support this audio player.
  </audio>
  <p>Written equivalent: a short tone sounds near the start, followed by another around three seconds. There is no speech.</p>
  <p><a href="media/practice-tones.wav">Open the tone recording directly</a></p>
</body>
</html>
Browser previewDocument title: Hear Two Practice Tones

This preview shows the example in a browser. Save the complete code in local files to test links between pages and downloads. Same-page section links work here.

Expected result: a native player appears. Press Play, pause it, then seek to an earlier position if the browser provides seeking. Compare the two sounds with the written equivalent. Playing audio does not require a custom JavaScript button in this example.

Source choices and type labels

You can place source children inside audio instead of putting src directly on audio. This is useful when offering the same recording in multiple supported encodings. A media type, often called a MIME type, tells the browser what kind of resource is offered.

FormatCommon extensionCommon type label
MP3.mp3audio/mpeg
PCM WAV.wavaudio/wav
Ogg audio.oggaudio/ogg

Format and codec support can vary. A codec is a way of encoding and decoding media data. Renaming a WAV file to .mp3 does not create MP3 data. Supply genuinely converted alternatives when needed; do not list imaginary fallback files.

When using source children, omit audio's own src. Its src takes precedence over the source-list selection. The browser examines candidates in order and uses a playable resource. A type hint can help it skip formats it cannot play, but it does not fix a corrupt file or a wrong address.

Complete program: audio-source.html

Only one real source is needed for this exercise. The structure is ready for additional genuine formats later.

audio-source.html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Audio Source and Written Alternative</title>
</head>
<body>
  <h1>Read the sound as well as playing it</h1>
  <audio controls preload="none">
    <source src="media/practice-tones.wav" type="audio/wav">
    Your browser does not support this audio player.
  </audio>
  <h2>Written equivalent</h2>
  <p>Two short quiet tones sound during six seconds, near the start and around the middle. There are no spoken instructions.</p>
  <p><a href="media/practice-tones.wav" download>Download the practice sound (WAV)</a></p>
</body>
</html>
Browser previewDocument title: Audio Source and Written Alternative

This preview shows the example in a browser. Save the complete code in local files to test links between pages and downloads. Same-page section links work here.

Expected result: another native player, a written description and a direct file download. Preload="none" asks the browser to avoid preloading media before it is needed, so duration may not appear until loading starts. Test in your own browser rather than assuming the interface will always show the same initial time.

The player needs a real recording
  1. 1Audio: the playback element
  2. 2Controls: native buttons
  3. 3Src or source: the recording address
  4. 4Visible text: the written equivalent

Fallback-only text is not a reliable error message for a failed resource.

Common playback settings

AttributeMeaning
controlsOffer the native controls
preload="none"Hint not to preload the resource
preload="metadata"Hint to load information such as duration
preload="auto"Permit preloading more or all of the resource
loopRepeat after the recording ends
mutedStart with sound muted
autoplayRequest playback without the reader pressing Play

Preload is a hint, not a strict download limit. Browser settings, network policy and playback needs can override it. Controls, loop, muted and autoplay are boolean attributes. To turn one off in source, remove it; autoplay="false" still includes the autoplay request.

Browsers often block autoplay with sound. Muting may change what is allowed, but it is not a universal permission guarantee. Our teaching pages leave playback to the student. Unexpected sound can interrupt people and make a lesson harder to use.

A transcript is more than a backup button

A transcript is a written version of the meaningful spoken and non-speech content. For a spoken explanation, include the actual words, speaker changes where relevant and sounds needed to understand the message. Do not replace a five-minute lesson with “Teacher explains HTML”.

This sound example has no speech, so its equivalent describes the tones. A real teaching recording needs the equivalent of its actual content. Place that text where readers can find it without playing the audio. An audio element has no img-style alt attribute for this job.

Problem and solution: controls appear but there is no sound

Problem: the player appears, so a student assumes the source must be correct.

Solution: check whether playback time advances, whether the player or device is muted, whether the file opens directly, and whether the src and type are correct. A visible player proves the element exists; it does not prove that the recording loaded or that the device can output sound.

Common mistakes and useful tips

  • Uploading the HTML but forgetting the audio file.
  • Using audio/mp3 instead of the normal MP3 type audio/mpeg.
  • Adding source alternatives that do not exist.
  • Setting both audio src and a competing source list without understanding precedence.
  • Hiding the only written alternative inside fallback content.
  • Assuming preload="none" forbids all future downloads.
  • Starting sound automatically or leaving students with no clear playback control.

Practice questions

  1. What does controls request?
  2. Why does a visible player not prove that the file loaded?
  3. What is the difference between audio src and source children?
  4. Does autoplay="false" turn autoplay off?
  5. Where should a useful transcript be available?
  6. Save the audio example and its media folder. Test play, pause, volume and the written alternative. Temporarily change the filename and explain how the failure differs from an unsupported audio element.
Check your practice answers
  1. The browser's native playback interface.
  2. The element can render even when its resource is missing, unsupported or unavailable.
  3. Src supplies a direct resource. Source children offer candidate resources when the audio element has no own src.
  4. No. Autoplay is boolean; remove the attribute.
  5. In visible, reachable page content or a clear transcript link, not only inside unsupported-element fallback.
  6. Keep the supplied WAV at its matching path. A broken resource in a supporting browser may leave a failed player without revealing the fallback sentence. Restore the filename afterwards.

Interview questions

Is JavaScript required for a basic audio player?

No. Audio with controls and a playable resource provides basic native playback. Custom behaviour and custom controls are separate tasks that require additional work.

Is preload a command that browsers must follow exactly?

No. It communicates the author's loading preference. Browsers can make different loading decisions, and requested playback needs the media data.

Why put a file link outside audio?

It stays visible in supporting browsers too, including when playback fails. A link inside fallback content is not a reliable resource-error message for modern browsers.

What should an accessible audio lesson include?

Usable playback controls and a written equivalent of its meaningful content. Test keyboard use and avoid making unexpected automatic sound the only way to receive instructions.

Quick revision

  • Audio supplies a player; the resource supplies the sound.
  • Use controls, real source files and correct type labels.
  • Boolean settings depend on presence, and preload is a hint.
  • Keep transcripts and useful links outside fallback-only content.