HTML Audio


Lesson 18 of 58

The HTML <audio> element lets you embed sound files directly into your web pages. No extra plugins needed. Just pure HTML.


The audio Element

To add audio to your page, use the <audio> tag. You can specify multiple audio formats so the browser can pick the one it supports.

Basic Audio Example

The controls attribute adds play, pause, and volume buttons. Always include it so users can control the audio.


How It Works

You put <source> tags inside the <audio> element. Each <source> points to a different audio format. The browser picks the first one it can play.

Audio with Multiple Sources
Tip: Always provide an MP3 version. It works in all modern browsers. OGG or WAV are good backups.

Autoplay Audio

You can make audio start automatically with the autoplay attribute. However, most browsers block autoplay with sound. To autoplay, you must also mute the audio.

Autoplay (Muted) Example
Note: Chrome, Edge, Safari, and Firefox block autoplay with sound. To autoplay, always add the muted attribute along with autoplay.

Loop and Preload Attributes

The loop attribute makes the audio repeat. The preload attribute tells the browser how to load the audio file.

  • preload="auto" – Loads the audio file when the page loads (default).
  • preload="metadata" – Loads only metadata (duration, etc.).
  • preload="none" – Does not load the audio until the user presses play.
Loop Example

Supported Audio Formats

There are three main audio formats supported by HTML. MP3 is the safest choice because it works everywhere.

Format File Extension Browser Support
MP3.mp3✅ All browsers (recommended)
OGG.ogg✅ All modern browsers
WAV.wav✅ All browsers (large file size)

Media Types (MIME Types)

When using <source>, always specify the type attribute with the correct MIME type.

File Format Media Type
MP3audio/mpeg
OGGaudio/ogg
WAVaudio/wav

Controlling Audio with JavaScript

You can control audio with JavaScript. This example uses buttons to play, pause, and reset the audio.

JavaScript Audio Controls

Quick Summary

  • Use <audio> to embed audio. Always include controls.
  • Use <source> inside to provide multiple formats. MP3 is the safest.
  • Use autoplay muted if you want audio to start automatically.
  • Use loop to repeat the audio.
  • Use preload to control how the audio loads.
  • You can control audio with JavaScript using .play(), .pause(), and .currentTime.

What Just Happened? Let Me Explain

  • The <audio> tag is supported in all modern browsers. You no longer need Flash or other plugins.
  • MP3 is the most compatible format. Use it for all your web audio.
  • Browsers block autoplay with sound to protect users from unexpected noise. If you want autoplay, mute the audio.
  • The text inside the <audio> tag only shows in very old browsers that do not support the audio tag.
  • WAV files are uncompressed, so they are very large. Use MP3 or OGG for better performance.
Tip: Always compress your audio files before uploading. Large audio files slow down your website. Tools like Audacity or online compressors can make audio files much smaller without losing quality.

HTML Audio Reference

Tag / Attribute Description
<audio>Defines sound content
<source>Defines multiple media resources for <audio> and <video>
controlsAdds play, pause, and volume buttons
autoplayStarts the audio automatically
mutedMutes the audio (required for autoplay)
loopRepeats the audio when it ends
preloadSpecifies if and how the audio should be loaded

Lesson 18 of 58