Introduction
The ‘<audio>
‘ element in HTML is used to embed audio content, such as music or sound effects, directly into a web page. It allows you to include audio files that can be played by the user’s browser without the need for external plugins. The ‘<audio>
‘ element supports various audio formats, and you can provide multiple sources to ensure compatibility with different browsers.
Here is a basic example of how the ‘<audio>
‘ element is typically used :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<audio controls>
<source src="on_my_way_hd_s1_hs.mp3">
</audio>
</body>
</html>
Output :
In this example:
- The
controls
attribute adds audio playback controls, such as play, pause, and volume. - The ‘
<source>
‘ element inside ‘<audio>
‘ specifies the audio file (audio-file.mp3
) and its type (audio/mp3
). You can provide multiple ‘<source>
‘ elements with different file formats to ensure compatibility across browsers.
Additional attributes you can use with the <audio>
element include:
- autoplay: Automatically starts playing the audio when the page loads.
- loop: Causes the audio to loop continuously.
- preload: Specifies whether the audio should be preloaded. Values can be “auto,” “metadata,” or “none.”
In this way we use ‘<audio>‘ tag.