Skip to content

HTML Audio

Modern HTML provides a standard way to embed and control audio playback directly in web pages.

Historically, playing audio on web pages required browser plugins like Adobe Flash.

The HTML <audio> element introduced a standardized, plugin-free method for embedding audio content.

The <audio> element is supported by all modern web browsers (Chrome, Firefox, Safari, Edge).

Support for specific audio formats may vary slightly, but common formats like MP3, AAC, Vorbis (in Ogg or WebM), Opus (in Ogg or WebM), and WAV are widely supported.

To embed an audio file, use the <audio> element. It’s recommended to provide multiple <source> elements for different formats to maximize browser compatibility.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Audio Example</title>
</head>
<body>
<h1>Listen to the Audio</h1>
<audio controls>
<source src="audio/sound.opus" type="audio/opus">
<source src="audio/sound.ogg" type="audio/ogg">
<source src="audio/sound.mp3" type="audio/mpeg">
Your browser does not support the audio element.
Please upgrade your browser or <a href="audio/sound.mp3">download the file</a>.
</audio>
</body>
</html>
  • <audio> element: The container for the audio player.
  • controls attribute: Adds default browser audio controls (play/pause, volume, timeline). Omitting this means you’ll need to create custom controls using JavaScript.
  • <source> elements: Specify different audio file sources. The browser will use the first format it supports.
  • src attribute: The URL of the audio file.
  • type attribute: The MIME type of the audio file. This helps the browser quickly determine if it can play the file without downloading it first.
  • Fallback Content: Text or links placed between the opening <audio> and closing </audio> tags will only be displayed in browsers that do not support the <audio> element at all (very rare today).
FormatMIME TypeNotes
MP3audio/mpegWidely supported, but patent-encumbered (though patents largely expired).
AACaudio/aacGood quality, common in Apple ecosystems.
Ogg Vorbisaudio/oggOpen source, good quality, widely supported (often in .ogg container).
Ogg Opusaudio/opusOpen source, excellent quality, very efficient, great for streaming and voice (often in .opus or .ogg container).
WAVaudio/wavUncompressed, high quality, large file size.
WebM Audio (Vorbis/Opus)audio/webmOpen container format, typically uses Vorbis or Opus audio codecs.

Recommendation: Provide Opus or Vorbis (in Ogg/WebM) for modern browsers and MP3 as a fallback for broader compatibility.

  • autoplay: Starts playing the audio automatically on page load. (Note: Most modern browsers block autoplay unless the audio is muted or the user has interacted with the page).
  • loop: Repeats the audio automatically when it finishes.
  • muted: Starts the audio muted.
  • preload: Hints to the browser how much audio data should be preloaded (none, metadata, auto).
<audio controls loop muted preload="metadata">
<!-- sources -->
</audio>

HTML provides a powerful JavaScript API to control audio playback dynamically. You can create custom controls, respond to events, and manage playback state.

Key methods include play(), pause(). Key properties include currentTime, duration, volume, paused, ended. Key events include play, pause, ended, timeupdate, volumechange.

Example (Conceptual):

<audio id="myAudio" src="audio.mp3"></audio>
<button onclick="document.getElementById('myAudio').play()">Play</button>
<button onclick="document.getElementById('myAudio').pause()">Pause</button>

For a comprehensive guide to the JavaScript API, refer to the MDN HTMLMediaElement documentation.

Ensure your audio content is accessible:

  • Provide transcripts for spoken content.
  • Use the <track> element to add captions or subtitles if applicable.
  • Ensure controls are keyboard navigable and usable with screen readers.
TagDescription
<audio>Defines embedded sound content.
<video>Defines embedded video content.
<source>Specifies multiple media resources for <audio> and <video>.
<track>Specifies timed text tracks (subtitles, captions) for media elements.