The <audio> tag in HTML allows you to embed audio content directly into your web pages, making it easy to include music, sound effects, or other audio streams. This guide will walk you through the basics of using the <audio> tag and its various attributes, providing insights into its functionality and browser compatibility.
Here's a simple example of how to use the <audio> tag to play an audio file:
<audio controls>
<source src="horse.ogg" type="audio/ogg">
<source src="horse.mp3" type="audio/mpeg">
Your browser does not support the audio tag.
</audio>
This code includes two source files, one in OGG format and another in MP3 format. The browser will choose the first supported format. If neither is supported, the fallback message "Your browser does not support the audio tag." will be displayed.
The <audio> tag supports several popular audio formats, including MP3, WAV, and OGG. However, browser support for these formats can vary. The following table outlines browser compatibility for each format:
Browser | MP3 | WAV | OGG |
---|---|---|---|
Edge / IE | YES | YES* | YES* |
Chrome | YES | YES | YES |
Firefox | YES | YES | YES |
Safari | YES | YES | NO |
Opera | YES | YES | YES |
*From Edge 79
To ensure maximum compatibility, it's recommended to provide multiple audio sources in different formats using the <source> tag. For instance, you could provide an MP3 file for browsers that support it and an OGG file as an alternative.
The <audio> tag comes with several useful attributes that give you greater control over the audio playback experience. Here's a breakdown of some key attributes:
Ask anything...