HTML Audio

What is HTML Audio Tag

The HTML <audio> tag is used to embed audio files on a web page. It can be used to play a variety of audio formats such as MP3, OGG, and WAV. The <audio> tag supports various attributes, such as src (to specify the source of the audio file), controls (to display the default audio player controls), and loop (to loop the audio file continuously). An example of an <audio> tag is:

<audio src="example.mp3" controls></audio>

This will embed an audio file called “example.mp3” and display the default audio player controls on the web page.

Example

<!DOCTYPE html>
<html>

   <head>
      <title>HTML audio Tag</title>
   </head>

   <body>
      <p>Click the button to listen audio...</p>
      <p>(Song: pixabay)</p>
      
      <audio controls>
         <source src = "local.mp3" type = "audio/mpeg">
      </audio>
   </body>

</html>

Output

Here is a table that compares the HTML <audio> tag and the <video> tag:

Feature<audio> Tag<video> Tag
PurposeEmbed audio filesEmbed video files
Supported file formatsMP3, OGG, WAVMP4, OGG, WEBM
Default player controlsAvailableAvailable
AutoplaySupportedSupported
LoopSupportedSupported
Width and HeightNot supportedSupported
Poster ImageNot supportedSupported

It is worth mentioning that <audio> and <video> tags both support the src, controls, autoplay, loop, muted, preload attributes, and some events like onplay, onpause, onended and onvolumechange .

It’s also worth noting that the <audio> tag is used for audio only, while the <video> tag can include both audio and video.

HTML Audio Tag Attributes

There is given a list of HTML audio tag.

AttributeDescription
controlsIt defines the audio controls which is displayed with play/pause buttons.
autoplayIt specifies that the audio will start playing as soon as it is ready.
loopIt specifies that the audio file will start over again, every time when it is completed.
mutedIt is used to mute the audio output.
preloadIt specifies the author view to upload audio file when the page loads.

HTML Audio
  1. The HTML <audio> tag is used to embed audio files on a web page.
  2. The <audio> tag supports various attributes, such as src (to specify the source of the audio file), controls (to display the default audio player controls), and loop (to loop the audio file continuously).