HTML Audio

HTML Audio Elements

Create rich audio experiences with HTML5 audio elements. Master playback controls, format optimization, accessibility features, and custom player implementations.

How do you play audio in HTML?

Use <audio controls src="song.mp3"></audio> to show a player. List several <source> elements to offer different formats, and the browser picks the first one it can play. Audio won't autoplay with sound; it needs a user interaction or the muted attribute.

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

HTML Audio — step by step

1 / 4
1

Add audio with the audio element

The audio element embeds sound content. Use the src attribute for a single file or nest source elements for multiple formats. Add controls to show native browser playback controls — without it the audio is invisible and inaccessible.

1 of 4
⚠️Autoplay Is Blocked Without User Interaction
Most browsers block autoplay of audio unless the user has previously interacted with the site or the audio is muted. Attempting to play audio on page load without interaction will throw a promise rejection. Always call play() in response to a user action like a click event.

🎯 Quick Check

Which attribute is required to display playback controls for an audio element?

HTML5 Audio Mastery

HTML5 audio elements provide native browser support for sound playback without plugins. Modern web applications leverage audio for podcasts, music players, sound effects, notifications, and interactive experiences.

Playback

Native controls

Formats

Multiple codecs

Control

Volume & muting

Custom

Player interfaces

Audio Element Features

🎵 Basic Playback

  • • Native browser audio controls
  • • Play, pause, and stop functionality
  • • Volume control and muting
  • • Progress tracking and seeking
  • • Loop and autoplay options
  • • Multiple format support

⚙️ Advanced Control

  • • JavaScript API for custom players
  • • Event handling for playback states
  • • Preload strategies for performance
  • • Crossfade and gapless playback
  • • Playlist management
  • • Real-time audio processing

🔊 Audio Formats

  • • MP3: Universal compatibility
  • • OGG Vorbis: Open source, quality
  • • AAC: High efficiency encoding
  • • WAV: Uncompressed quality
  • • Opus: Modern, low latency
  • • FLAC: Lossless compression

♿ Accessibility

  • • Text transcripts for audio content
  • • Keyboard navigation support
  • • Screen reader compatibility
  • • Clear visual control indicators
  • • Respect user preferences
  • • Alternative content formats

Complete Audio Implementation

Explore comprehensive audio implementation with multiple formats, custom controls, and accessibility features:

Advanced audio player with custom controls and playlist:

Code Editor

<audio id="player" controls preload="metadata"
       src="https://interactive-examples.mdn.mozilla.net/media/cc0-audio/t-rex-roar.mp3">
  Your browser does not support audio.
</audio>
<p>
  <button id="play">Play / Pause</button>
  <label>Volume <input id="vol" type="range" min="0" max="1" step="0.1" value="1"></label>
</p>

Preview

Implementation Features:

  • • Multiple audio format support
  • • Custom player with JavaScript controls
  • • Playlist functionality with auto-advance
  • • Volume control and muting
  • • Accessibility considerations
  • • Performance optimization strategies

Advanced Techniques:

  • • Event-driven playback control
  • • Dynamic source switching
  • • Progress tracking and seeking
  • • Custom styling and branding
  • • Mobile-optimized interactions
  • • Browser compatibility handling

Performance & Best Practices

Best Practices:

  • Provide multiple format sources
  • Use appropriate preload strategies
  • Optimize file sizes and bitrates
  • Include accessibility features
  • Test across different browsers

Common Pitfalls:

  • Ignoring autoplay policies on mobile
  • Using only one audio format
  • Not providing fallback content
  • Neglecting accessibility requirements
  • Oversized files without optimization

Performance Tips

File Optimization:

  • • Choose appropriate bitrates
  • • Use variable bitrate encoding
  • • Consider modern codecs (Opus)
  • • Compress metadata

Loading Strategy:

  • • Use preload="metadata" for lists
  • • Implement lazy loading
  • • Cache frequently accessed files
  • • Progressive download for long content

User Experience:

  • • Provide loading indicators
  • • Respect autoplay preferences
  • • Optimize for mobile data usage
  • • Handle network interruptions

Browser Compatibility

Audio Format Support Matrix

FormatChromeFirefoxSafariEdgeMobile
MP3✓ Full✓ Full✓ Full✓ Full✓ Excellent
OGG Vorbis✓ Yes✓ Yes✗ No✓ Yes⚠️ Limited
AAC✓ Yes⚠️ Limited✓ Yes✓ Yes✓ Good
Opus✓ Modern✓ Modern⚠️ Recent✓ Modern⚠️ Newer

Recommended Fallback Strategy

<audio controls>
<source src="audio.opus" type="audio/opus">
<source src="audio.ogg" type="audio/ogg">
<source src="audio.mp3" type="audio/mpeg">
<p>Your browser doesn't support HTML5 audio.</p>
</audio>

This strategy provides modern formats first, falling back to widely supported MP3, with a final fallback message for unsupported browsers.

Audio Excellence Achieved! 🎵

Fantastic! You've mastered HTML audio elements with comprehensive coverage of formats, controls, accessibility, and performance optimization. You're ready to create professional audio experiences. Let's continue with video elements!

HTML Audio: summary

Key points

  • MP3 and AAC (M4A) play everywhere; Opus and Ogg are smaller where supported.
  • Control playback from JavaScript with audio.play(), pause(), currentTime and volume.
  • Provide a transcript for spoken audio for accessibility and SEO.

Frequently asked questions

›How do I loop audio?

Add the loop attribute: <audio src="bg.mp3" loop controls>.