HTML <audio> Tag
To specify sounds (music and other audio clips), the HTML <audio> tag is used. Three types of file formats are supported for HTML 5 audio tags currently. These are mp3, wav and ogg. Both the <video> and <audio> controls are supported in HTML 5. To play the multimedia items, technologies like Flash, Silverlight, and other similar technologies are used.
Supported audio file format:
Browser | mp3 | wav | ogg |
Internet Explorer | Yes | No | No |
Chrome | Yes | Yes | Yes |
Firefox | Yes | Yes | Yes |
Opera | No | Yes | Yes |
Safari | Yes | Yes | No |
Example 1:
<!DOCTYPE html> <html> <body> <p>Play the Audio.</p> <audio controls> <source src="peacock.mp3" type="audio/mpeg"> This browser does not support the audio element. </audio> </body> </html>
Explanation:
In the above example, we are playing an mp3 file using an HTML audio tag.
Example 2:
<!DOCTYPE html> <html> <body> <p>Play the Audio.</p> <audio controls> <source src="peacock.ogg" type="audio/ogg"> This browser does not support the audio element. </audio> </body> </html>
Explanation:
In the above example, we are playing an Ogg file using an HTML audio tag.
Supporting Browsers:
Chrome, IE, Firefox, Opera, and Safari.
Attributes of HTML <audio> Tag:
Attribute | Uses |
controls | Used to specify the audio controls and is displayed with play/pause buttons. |
autoplay | Used to specify that the audio is on autoplay mode. |
loop | Used to start the audio file over and over again, every time when it is completed. |
muted | Used to mute the audio output. |
preload | Used to specify the author view to load the audio file when the page loads. |
src | Used to specify the URL of the audio file to add. |
Example 3:
<!DOCTYPE html> <html> <body> <p>Play the Audio.</p> <audio controls autoplay loop> <source src="peacock.mp3" type="audio/mpeg"> This browser does not support the audio element. </audio> </body> </html>
Explanation:
In the above example, we are playing an mp3 file using controls, autoplay, loop, and src attributes of the HTML audio tag.
MIME types for HTML <audio> tag:
Audio Format | MIME Type |
mp3 | audio/mpeg |
ogg | audio/ogg |
wav | audio/wav |