The <audio> tag in HTML5 is used to embed audio content on a webpage. It allows you to play audio files like MP3, OGG, or WAV directly in the browser. The <audio> element provides attributes for controlling playback, such as play, pause, and volume.
- 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.
<!--Driver Code Starts-->
<!DOCTYPE html>
<html>
<body>
<!--Driver Code Ends-->
<audio controls>
<source src=
"https://media.geeksforgeeks.org/wp-content/uploads/20230524142525/gfg_offline_classes_en.mp3"
type="audio/mp3">
<source src=
"https://media.geeksforgeeks.org/wp-content/uploads/20220913101124/audiosample.ogg"
type="audio/mp3">
</audio>
<!--Driver Code Starts-->
</body>
</html>
<!--Driver Code Ends-->
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:
Attributes | Description |
|---|---|
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 be muted. | |
Designates the URL of the audio file. | |
Designates that the audio file should continuously repeat. |
Autoplay audio
The autoplay attribute is used to automatically begin playback of the audio file whenever the URL of the webpage is loaded.
<!--Driver Code Starts-->
<!DOCTYPE html>
<html>
<body>
<!--Driver Code Ends-->
<audio controls autoplay>
<source src=
"https://media.geeksforgeeks.org/wp-content/uploads/20230524142525/gfg_offline_classes_en.mp3"
type="audio/mp3">
<source src=
"https://media.geeksforgeeks.org/wp-content/uploads/20220913101124/audiosample.ogg"
type="audio/mp3">
</audio>
<!--Driver Code Starts-->
</body>
</html>
<!--Driver Code Ends-->
Muted Audio
The muted attribute specifies that the audio should be muted on the webpage.
<!--Driver Code Starts-->
<!DOCTYPE html>
<html>
<body>
<!--Driver Code Ends-->
<audio controls muted>
<source src=
"https://media.geeksforgeeks.org/wp-content/uploads/20230524142525/gfg_offline_classes_en.mp3"
type="audio/mp3">
<source src=
"https://media.geeksforgeeks.org/wp-content/uploads/20220913101124/audiosample.ogg"
type="audio/mp3">
</audio>
<!--Driver Code Starts-->
</body>
</html>
<!--Driver Code Ends-->
Audio with Multiple Src
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.
<!--Driver Code Starts-->
<!DOCTYPE html>
<html>
<body>
<!--Driver Code Ends-->
<audio controls autoplay>
<source src="test.mp3" type="audio/mp3">
<source src="test.ogg" type="audio/ogg">
<source src=
"https://media.geeksforgeeks.org/wp-content/uploads/20230524142525/gfg_offline_classes_en.mp3"
type="audio/mp3">
</audio>
<!--Driver Code Starts-->
</body>
</html>
<!--Driver Code Ends-->
Add audio using "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.
<!DOCTYPE html>
<html>
<body>
<embed src=
"https://media.geeksforgeeks.org/wp-content/uploads/20230524142525/gfg_offline_classes_en.mp3"
width="600" height="200"
autoplay="true" loop="true">
</body>
</html>