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.
Example02
🌐Code Cell
Loading Editor...
✏️ You can edit this code — click Run to refresh the preview.
Output

▶️ Autoplay Behavior

Modern browsers usually require audio to be muted or user-initiated before autoplay is allowed.
Example03
🌐Code Cell
Loading Editor...
✏️ You can edit this code — click Run to refresh the preview.
Output

🎼 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