Home » HTML » HTML Audio Tag

HTML Audio Tag

The HTML5 <audio> tag is used to embed audio in a web page. It's attributes works pretty similar to that of video tag. Not all browsers support all audio formats so, the audio file is encoded using special codecs. It is recommended to use .mp3 file format as it is the most popular audio format globally.

Example to play mp3 file using audio tag in HTML:


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> HTML Audio Tag </title>
</head>
<body>
<audio controls>
<source src="html-audio.ogg" type="audio/ogg">
<source src="html-audio.mp3" type="audio/mpeg">
Your browser does not support the audio element.
</audio>
</body>
</html>

Output

HTML Audio Tag

How it Works ?

The controls attribute in audio adds controls, like play, pause, and volume. The <source> element allows you to specify alternative audio files which the browser may choose from. The browser will use the first recognized format. The text between the <audio> and </audio> tags will only be displayed in browsers if the <audio> element are not support.

HTML Audio Formats

Audio File Format Media Type
.mp3 audio/mp3
.ogg audio/ogg
.wav audio/wav

Link Audio Files


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> Linking Audio Files </title>
</head>
<body>
<p><a href="html-audio.mp3" width="200px" height="50px"> Audio 1 </a></p>
<p><a href="html-audio.ogg" width="200px" height="50px"> Audio 2 </a></p>
</body>
</html>

Output

Linking Audio Files

Audio 1

Audio 2


HTML5 Audio Attributes

Tags Description
controls It defines the audio controls with play/pause buttons.
autoplay It specifies that the audio will start playing as soon as it is ready.
loop It specifies that the audio file will start over again, every time when it is completed.
muted It is used to mute the audio output.
preload It specifies the author view to upload audio file when the page loads. It is ignored if autoplay is used.
src It specifies the address of the audio file.

Browser Support

Element
Microsoft Edge browser.png Edge
Chrome browser.png Chrome
Firefox browser.png Firefox
Opera browser.png Opera
safari browser.png Safari
.mp3
Yes
Yes
Yes
Yes
Yes
.ogg
Yes
Yes
Yes
Yes
No
.WebM
Yes
Yes
Yes
Yes
No











Follow Us: