<audio> Tag in HTML
The <audio> tag in HTML is used to embed audio content, such as music or sound files, in a web page. It allows you to control the playback of audio directly from the browser without relying on external plugins.
Key Points on <audio> Tag:
- The
<audio>
tag can include multiple audio sources using the<source>
tag for better compatibility across browsers. - It provides built-in controls, such as play, pause, and volume adjustment, when the
controls
attribute is used. - The
autoplay
attribute allows the audio to begin playing as soon as it is ready, while theloop
attribute restarts the audio when it ends. - The
preload
attribute specifies how the audio file should be loaded (e.g., "auto", "metadata", or "none").
Syntax of <audio> Tag:
Syntax Example
<audio controls>
<source src="audiofile.mp3" type="audio/mp3">
Your browser does not support the audio element.
</audio>
Example of <audio> Tag in HTML:
This example demonstrates the use of the <audio>
tag to embed a Hindi song with playback controls.
Code Example
<audio controls>
<source src="https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3" type="audio/mp3">
Your browser does not support the audio element.
</audio>
Output
Attributes of <audio> Tag:
- controls: Displays the audio controls (play, pause, volume).
- autoplay: The audio starts playing automatically as soon as it's ready.
- loop: Makes the audio play repeatedly.
- muted: Mutes the audio on load.
- preload: Specifies how the audio file should be loaded (auto, metadata, or none).
The <audio>
tag is supported by most modern browsers, but it is always a good practice to provide fallback content or multiple audio formats (e.g., MP3, Ogg) for broader compatibility.