<audio>

The HTML <audio> element embeds audio content, like music or sound files, into a webpage, offering controls for playback, volume, and track duration.

audio tag

The <audio> element embeds sound content, such as music or voice recordings, into a webpage, allowing users to play audio files directly in their browsers. The <audio> element supports formats like MP3, WAV, and Ogg.

Basic <audio> Example

index.html
<audio controls>
  <source src="song.mp3" type="audio/mpeg" />
</audio>

Keys

  • controls: Adds audio playback controls.
  • autoplay: Starts audio automatically.
  • muted: Mutes the audio by default.

HTML Audio with Controls and Autoplay

index.html
<audio controls autoplay>
  <source src="song.mp3" type="audio/mpeg" />
</audio>
Most Chromium-based browsers restrict autoplay in many situations. However, autoplay with the sound muted is always permitted.

To make your audio file play automatically without sound, include the muted attribute alongside autoplay.

index.html
<audio controls autoplay muted>
  <source src="song.mp3" type="audio/mpeg" />
</audio>

Supported Audio Formats in HTML

Web browsers support different audio formats, including MP3, WAV, and OGG. The table below shows which formats are compatible with various browsers:

Browser Support for Audio Formats

BrowserMP3WAVOGG
Edge/IEYESYES*YES*
ChromeYESYESYES
FirefoxYESYESYES
SafariYESYESNO
OperaYESYESYES

Media Types for Audio Files

Different file formats correspond to specific media types in HTML. The table below lists the most common ones:

Audio File Formats and Media Types

File FormatMedia Type
MP3audio/mpeg
OGGaudio/ogg
WAVaudio/wav

Audio Methods, Properties, and Events

The HTML DOM provides various methods and properties for the <audio> element. These allow you to control playback, adjust volume, and determine the duration of an audio file. Additionally, certain events trigger notifications when audio starts playing, pauses, or stops.

HTML Audio Tags

Below are the key tags used for embedding audio in HTML:

HTML Audio Elements

TagDescription
<audio>Defines an audio element for embedding sound.
<source>Specifies different audio files for compatibility with various browsers.

Conclusion

The <audio> tag in HTML is a straightforward and essential tool for embedding audio content on webpages. It supports various audio formats like MP3, WAV, and Ogg, ensuring compatibility across different browsers. Key attributes such as controls, and poster provide flexibility for user interaction and playback options.