HTML5 <audio> Tag
Last Updated :
07 Mar, 2024
The <audio> tag is an inline element that is used to embed sound files into a web page. Since the release of HTML5, audio can be added to web pages using the “audio” tag. It is a useful tag if you want to add audio such as songs, interviews, etc. on your webpage.
Previously, audio could be only played on web pages using web plugins like Flash. It is a useful tag if you want to add audio such as songs, interviews, etc. on your webpage.
Syntax:Â
<audio>
<source src="sample.mp3" type="audio/mpeg">
</audio>
Attributes:
The various attributes that can be used with the “audio” tag are listed below:
Designates what controls to display with the audio player.
|
Designates that the audio file will play immediately after it loads controls.
|
Designates that the audio file should continuously repeat.
|
Designates the URL of the audio file.
|
Designates that the audio file should be muted.
|
Understanding HTML Audio :
- Using the <source> element enables the specification of various audio files, allowing the browser to choose the compatible format.
- The <audio> element allows integration with JavaScript, enabling the creation of custom audio controls and behaviors.
- The ‘controls’ attribute provides buttons for managing audio, like play, pause, and volume adjustment.
- Any text contained between the <audio> tags is visible only on browsers that can’t render the <audio> element.
Supported Formats:Â
Autoplay is disabled in most Chromium browsers, but muted autoplay is still allowed. Three formats mp3, ogg, and wav are supported by HTML5. The support for each format by different browsers is given below :Â
Yes |
Yes |
Yes |
Yes |
No |
No |
Yes |
Yes |
Yes |
Yes |
Yes |
Yes |
Yes |
Yes |
No |
Example 1 (Adding audio to Webpage): The controls attribute is used to add audio controls such as play, pause, and volume. The “source” element is used to specify the audio files which the browser may use. The first recognized format is used by the browser.
HTML
<!DOCTYPE html>
< html >
< body >
< p >Audio Sample</ p >
< audio controls>
< source src =
type = "audio/mp3" >
< source src =
type = "audio/mp3" >
</ audio >
</ body >
</ html >
|
Output:Â
Example 2 (Autoplaying audio on a Webpage): The autoplay attribute is used to automatically begin playback of the audio file whenever the URL of the webpage is loaded.
HTML
<!DOCTYPE html>
< html >
< body >
< p >Audio Sample</ p >
< audio controls autoplay>
< source src =
type = "audio/mp3" >
< source src =
type = "audio/mp3" >
</ audio >
</ body >
</ html >
|
Output:Â
Example 3 (Adding muted audio file on a Webpage):Â The muted attribute specifies that the audio should be muted on the webpage.
HTML
<!DOCTYPE html>
< html >
< body >
< p >Audio Sample</ p >
< audio controls muted>
< source src =
type = "audio/mp3" >
< source src =
type = "audio/mp3" >
</ audio >
</ body >
</ html >
|
Output:Â
Example 4 (Adding audio using the source element):Â The source element can be used to add audio files to a webpage. The src attribute is used to specify the source of the file specified.
HTML
<!DOCTYPE html>
< html >
< body >
< p >Audio Sample</ p >
< audio controls autoplay>
< source src =
type = "audio/mp3" >
</ audio >
</ body >
</ html >
|
Output:Â
Example 5 (Adding audio with multiple sources): Multiple sources of audios are specified so that if the browser is unable to play the first source, then it will automatically jump to the second source and try to play it.
HTML
<!DOCTYPE html>
< html >
< body >
< p >Audio Sample</ p >
< audio controls autoplay>
< source src = "test.mp3" type = "audio/mp3" >
< source src = "test.ogg" type = "audio/ogg" >
< source src = "test.opus" type = "audio/ogg" >
</ audio >
</ body >
</ html >
|
Output :Â
Example 6 (Adding audio using the “Embed” tag): Adding audios to a webpage using the “embed” tag is an old technique. This method does work, but is comparatively less efficient than the other methods. The user must have a plugin like MIDI or QuickTime because the embed tag requires a plugin for support.
HTML
<!DOCTYPE html>
< html >
< body >
< p >Audio Sample</ p >
< embed src =
width = "600" height = "200"
autoplay = "true" loop = "true" >
</ body >
</ html >
|
Output:
Supported browsers: Â
- Google Chrome 15
- Edge 12 and above
- Firefox 1 and above
- Opera 14 and above
- Safari 6 and above
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...