The ‘<audio>‘ element in HTML is used to embed sound content, such as music or other audio streams, in web pages. It is a versatile element that provides several options for controlling playback and presenting the audio content to users.

Key Attributes of the

  1. src‘:

    • Specifies the URL of the audio file.
    • Example:’<audiosrc="audiofile.mp3"></audio>
  2. controls:

    • Adds audio controls, such as play, pause, and volume, to the player.
    • Example:’<audiosrc="audiofile.mp3" controls></audio>
  3. autoplay:

    • Automatically starts playing the audio as soon as it is ready.
    • Example:’<audiosrc="audiofile.mp3"autoplay></audio>
  4. loop:

    • Plays the audio in a loop, repeating it continuously.
    • Example:’<audiosrc="audiofile.mp3" loop></audio>
  5. muted:

    • Initially loads the audio in a muted state.
    • Example:’<audiosrc="audiofile.mp3" muted></audio>
  6. preload:

    • Instructs the browser on how to load the audio when the page loads.
    • Values:
      • auto‘: Loads the entire audio file.
      • metadata‘: Only loads the audio metadata.
      • none‘: Does not load the audio file.
    • Example:'<audiosrc="audiofile.mp3" preload="auto"></audio>
  7. type:

    • Defines the MIME type of the audio file (e.g.,’audio/mpeg,audio/ogg').

Example of the 'audio' Element with Multiple Sources:

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

				
			
  • The ‘<source>‘ elements provide different formats of the audio file to ensure compatibility across different browsers.
  • The text “Your browser does not support the audio element.” will be displayed if the browser does not support the ‘<audio>‘ element.

Browser Support:

  • Modern browsers like Chrome, Firefox, Safari, and Edge fully support the <audio> element.
  • The support for specific audio formats (like MP3, Ogg, WAV) may vary depending on the browser.

Example with Autoplay, Loop, and Muted:

				
					<audio src="background-music.mp3" autoplay loop muted></audio>

				
			
  • The audio will start playing automatically (‘autoplay‘).
  • It will loop indefinitely (‘loop‘).
  • The audio will start muted (‘muted‘).

Advanced Example with JavaScript Control:

				
					<audio id="myAudio" controls>
  <source src="audiofile.mp3" type="audio/mpeg">
  <source src="audiofile.ogg" type="audio/ogg">
</audio>

<button onclick="playAudio()">Play</button>
<button onclick="pauseAudio()">Pause</button>
<button onclick="stopAudio()">Stop</button>

<script>
  var audioElement = document.getElementById('myAudio');
  
  function playAudio() {
    audioElement.play();
  }

  function pauseAudio() {
    audioElement.pause();
  }

  function stopAudio() {
    audioElement.pause();
    audioElement.currentTime = 0;
  }
</script>

				
			
  • JavaScript is used to create custom buttons for playing, pausing, and stopping the audio.
  • The ‘play()‘ method starts the audio, ‘pause()‘ pauses it, and setting ‘currentTime‘ to ‘0‘ resets the audio.

Accessibility Considerations:

  • Ensure that the audio does not autoplay if it could be disruptive to users.
  • Provide controls and alternatives, such as captions or transcripts, for users who cannot hear the audio.
Scroll to Top