Nearby lessons

13 of 24

๐ŸŽต HTML <audio> Element

๐Ÿ“Œ What is HTML Audio?

The <audio> element in HTML is used to embed sound content such as music, podcasts, or sound effects. It supports attributes like controls, autoplay, loop, and audio formats like MP3, WAV, and Ogg.

๐ŸŽง Basic Usage

The <audio> tag allows you to play audio files directly in the browser with built-in controls.
Code Example
PREVIEW READY
Loading Editor...
Live Preview

โ–ถ๏ธ Autoplay Behavior

Modern browsers usually require audio to be muted or user-initiated before autoplay is allowed.
Code Example
PREVIEW READY
Loading Editor...
Live Preview

๐ŸŽผ Supported Audio Formats

AttributeDescription
MP3๐ŸŽฏ Most widely supported audio format across browsers
WAV๐Ÿ”Š High-quality audio with larger file size
Ogg๐ŸŒ Open format with good compression

โš™๏ธ Common Attributes

AttributeDescription
controlsShows audio controls like play, pause, and volume
autoplayStarts playing audio automatically (usually muted)
loopRepeats the audio when it ends
mutedStarts the audio with no sound
preloadSpecifies how the audio should be loaded: auto, metadata, none

๐Ÿงช Audio API (JavaScript)

  • Methods: play(), pause(), load()
  • Properties: currentTime, volume, duration
  • Events: play, pause, ended

โœ… Best Practices

  • โœ… Always include controls for better usability
  • โœ… Provide multiple audio formats for browser compatibility
  • โœ… Avoid autoplay unless necessary
  • โœ… Keep file sizes optimized for faster loading
  • โœ… Use meaningful fallback text for unsupported browsers

๐Ÿง  Test Your Knowledge

5 Questions

Progress: 0 / 5
Keep Going!HTML Video