Nearby lessons
13 of 24๐ต HTML <audio> Element
๐ What is HTML Audio?
The
<audio> element in HTML is used to embed sound content such as music, podcasts, or sound effects. It supports attributes like controls, autoplay, loop, and audio formats like MP3, WAV, and Ogg.๐ง Basic Usage
The
<audio> tag allows you to play audio files directly in the browser with built-in controls.Code Example
PREVIEW READY
โถ๏ธ Autoplay Behavior
Modern browsers usually require audio to be muted or user-initiated before autoplay is allowed.
Code Example
PREVIEW READY
๐ผ Supported Audio Formats
| Attribute | Description |
|---|---|
MP3 | ๐ฏ Most widely supported audio format across browsers |
WAV | ๐ High-quality audio with larger file size |
Ogg | ๐ Open format with good compression |
โ๏ธ Common Attributes
| Attribute | Description |
|---|---|
controls | Shows audio controls like play, pause, and volume |
autoplay | Starts playing audio automatically (usually muted) |
loop | Repeats the audio when it ends |
muted | Starts the audio with no sound |
preload | Specifies how the audio should be loaded: auto, metadata, none |
๐งช Audio API (JavaScript)
- Methods:
play(),pause(),load() - Properties:
currentTime,volume,duration - Events:
play,pause,ended
โ Best Practices
- โ
Always include
controlsfor better usability - โ Provide multiple audio formats for browser compatibility
- โ Avoid autoplay unless necessary
- โ Keep file sizes optimized for faster loading
- โ Use meaningful fallback text for unsupported browsers
๐ง Test Your Knowledge
5 QuestionsProgress: 0 / 5
Keep Going!HTML Video