HTML Audio

The <audio> element allows you to embed audio content such as music, podcasts, or sound effects. It supports various formats (MP3, Ogg, WAV) and controls for play, pause, and volume. Providing multiple sources ensures better browser compatibility, and fallback text can be included.

Key Topics

Common Audio Formats

MP3 and Ogg are widely supported. Provide multiple sources for best coverage.

Audio Attributes

Examples: controls, autoplay, loop, muted.

Audio Example

This example shows an audio player with two source formats and fallback text. A full code sample is provided below.

<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>

Explanation: The audio element tries MP3 first, then Ogg. If neither works, it shows fallback text. The controls attribute provides playback options to the user.

Key Takeaways

  • Use <audio> to embed sound content in HTML.
  • Include multiple sources for better compatibility.
  • Use controls to let users play, pause, and adjust volume.
  • Fallback text ensures graceful degradation if unsupported.
  • Test different formats and attributes to provide a seamless audio experience.