HTML5 <audio> Tag

15 Jul, 2022
Since the release of HTML5, audios can be added to webpages using the “audio” tag. Previously, audios could be only played on web pages using web plugins like Flash. The “audio” tag is an inline element that is used to embed sound files into a web page. 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:
Controls: Designates what controls to display with the audio player.
Autoplay: Designates that the audio file will play immediately after it loads controls.
Loop: Designates that the audio file should continuously repeat.
src: Designates the URL of the audio file.
muted: Designates that the audio file should be muted. 
Supported Formats: Three formats mp3, ogg, wav are supported by HTML5. The support for each format by different browsers is given below :  
The below Examples explain the audio Tag:
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 tag starts here -->    <audio controls>        <source src="test.mp3" type="audio/mp3">        <source src="test.ogg" type="audio/ogg">    </audio>    <!-- audio tag ends here --> </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 tag starts here -->    <audio controls autoplay>        <source src="test.mp3" type="audio/mp3">        <source src="test.ogg" type="audio/ogg">    </audio>    <!-- audio tag ends here --></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 tag starts here -->    <audio controls muted>        <source src="test.mp3" type="audio/mp3">        <source src="test.ogg" type="audio/ogg">    </audio>    <!-- audio tag ends here --></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 tag starts here -->    <audio controls autoplay>        <source src="test.mp3" type="audio/mp3">    </audio>    <!-- audio tag ends here --> </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 tag starts here -->    <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>    <!-- audio tag ends here --> </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 code starts here -->    <embed src="test.mp3" width="200" height="50"                     autoplay="true" loop="true">    <!-- embed code ends here --></body> </html>
Output:
Supported browsers:  
Google Chrome 3.0 and above
Edge 12.0 and above
Internet Explorer 9.0 and above
Firefox 3.5 and above
Opera 10.5 and above
Safari 3.1 and above
nidhi_biet
shubhamyadav4
sanayush1357
kumargaurav97520
HTML-Tags
HTML5
HTML
Technical Scripter
HTML
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.