<audio>
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
<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
<audio controls autoplay>
<source src="song.mp3" type="audio/mpeg" />
</audio>
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
.
<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
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 |
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 Format | Media Type |
---|---|
MP3 | audio/mpeg |
OGG | audio/ogg |
WAV | audio/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
Tag | Description |
---|---|
<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.
<video>
The HTML <video> element allows the embedding of a video file within a webpage, offering built-in playback controls and supporting various formats through nested <source> elements.
<embed>
The HTML <embed> element integrates external content, like multimedia or interactive applications, directly into a webpage, facilitating the inclusion of media without needing plugins.