HTML <audio> audio tag


The <audio> tag in HTML is used to embed sound content, such as music or audio files, into a webpage. It provides a way to include audio files that users can play directly within their web browsers. The <audio> tag supports various audio formats and offers built-in controls for playing, pausing, and adjusting the volume of the audio.

Syntax:

<audio src="audio-file.mp3" controls> Your browser does not support the audio element. </audio>

Attributes:

  1. src: Specifies the path to the audio file. This is the URL or file path of the audio you want to play.

    Example:

    <audio src="song.mp3" controls></audio>
  2. controls: Adds built-in controls to the audio player, such as play, pause, and volume adjustment. This attribute is optional; if omitted, the audio will not have any controls, and users will not be able to play or interact with it.

    Example:

    <audio controls> <source src="song.mp3" type="audio/mpeg"> <source src="song.ogg" type="audio/ogg"> Your browser does not support the audio element. </audio>
  3. autoplay: Makes the audio start playing automatically when the page loads. This attribute is optional and should be used with caution, as autoplaying audio can be disruptive to users.

    Example:

    <audio src="song.mp3" autoplay controls></audio>
  4. loop: Causes the audio to loop continuously once it finishes playing. This attribute is optional.

    Example:

    <audio src="song.mp3" loop controls></audio>
  5. preload: Specifies how the audio file should be loaded when the page loads. It can take one of three values:

    • auto: The browser should preload the entire audio file.
    • metadata: Only metadata (e.g., duration) should be loaded.
    • none: The audio file should not be preloaded.

    Example:

    <audio src="song.mp3" preload="metadata" controls></audio>
  6. muted: Mutes the audio. This attribute is optional and is useful for autoplay scenarios where you want the audio to be silent initially.

    Example:

    <audio src="song.mp3" muted autoplay controls></audio>

Example with Multiple Sources:

To ensure compatibility with different browsers, you can provide multiple audio sources using the <source> element:

<audio controls> <source src="song.mp3" type="audio/mpeg"> <source src="song.ogg" type="audio/ogg"> Your browser does not support the audio element. </audio>

How It Works:

  1. Browser Support: Most modern browsers support the <audio> tag, but support for specific formats can vary. Using multiple <source> elements helps ensure that the audio file plays in as many browsers as possible.
  2. Fallback Content: The text inside the <audio> tag (e.g., "Your browser does not support the audio element.") will be displayed if the browser does not support the <audio> tag.

Accessibility:

  • Controls: Providing the controls attribute makes the audio player accessible to users with disabilities, allowing them to play, pause, and adjust the volume.
  • Alternative Text: Although there is no built-in attribute for alternative text in the <audio> tag, providing clear and descriptive fallback content can help users understand what the audio is about if their browser does not support the tag.