HTML <audio> Element

Publish in HTML Tutorial el 23/05/2025 17:32

The <audio> element is used to embed sound content in web pages. It allows you to play audio files directly in the browser without requiring plugins. The element supports multiple audio formats (MP3, WAV, OGG) and provides built-in controls for play, pause, volume, and more.


Key attributes include:

  • src: Specifies the audio file URL
  • controls: Shows default audio controls
  • autoplay: Automatically plays the audio
  • loop: Makes the audio repeat
  • muted: Starts with audio muted
  • preload: Specifies if audio should be loaded when page loads


Basic Syntax

Here's the basic structure of the audio element:


Audio Element Examples


1. Basic Audio Player

This example shows a simple audio player with default controls.

Play Code


2. Autoplay and Loop

This audio will automatically play and loop continuously (note: many browsers block autoplay).

Play Code


3. Multiple Source Formats

Providing multiple source formats for better browser compatibility.

Play Code


4. Custom Controls with JavaScript

Using JavaScript to create custom controls for the audio player.

Play Code


5. Audio Events with JavaScript

Demonstrating how to use JavaScript audio events to create visual feedback.

Play Code


Tips and Tricks

  • Always include fallback content between the <audio> tags for browsers that don't support it
  • For better compatibility, provide multiple audio formats (MP3, OGG, WAV)
  • Use the preload="metadata" attribute to load only essential info initially
  • Consider user experience - don't autoplay audio without user consent
  • Use JavaScript to create custom styled players that match your site design
  • Leverage the Web Audio API for advanced audio manipulation and visualization

También te puede interesar

HTML <style> Element
HTML <style> Element

The <style> HTML element contains style information for a document, or part of a document. ...

HTML <strong> Element
HTML <strong> Element

The <strong> element is used to indicate that its content has strong importance, seriousnes...

HTML <span> Element
HTML <span> Element

The <span> element is an inline container used to mark up a part of a text or document. Unl...

HTML <source> Element
HTML <source> Element

The <source> element is used to specify multiple media resources for media elements like &l...

HTML <small> Element
HTML <small> Element

The <small> element is used to represent side-comments and small print, typically for discl...

HTML <section> Element
HTML <section> Element

HTML <section> Element: The Complete Guide The <section> element is a semantic HTML ...