HTML Audio
HTML Audio
The <audio> element in HTML is used to embed audio content on web pages. It allows users to play, pause, and control audio directly in the browser without additional plugins.
Basic Syntax
-
<source>→ Specifies the audio file and its format. -
controls→ Displays play, pause, and volume controls. -
Fallback text is displayed if the browser does not support
<audio>.
Audio Attributes
| Attribute | Description |
|---|---|
controls |
Displays audio controls for the user |
autoplay |
Starts playing the audio automatically |
loop |
Plays the audio continuously |
muted |
Starts the audio muted |
preload |
Specifies how the audio should be loaded (auto, metadata, none) |
Example: HTML Audio with Attributes
Explanation:
-
Audio starts autoplay, loops, and is muted by default.
-
Multiple
<source>tags ensure cross-browser compatibility. -
controlsallows the user to play, pause, and adjust volume.
Key Points
-
HTML5
<audio>eliminates the need for Flash or external plugins. -
Always include multiple audio formats (
mp3,ogg, etc.) for compatibility. -
Use
controlsfor better user experience. -
autoplayoften requiresmutedto work in modern browsers.
