HTML audio Tag: How to Embed and Control Sound in Pages

html audio tag

The audio tag adds sound to web pages without plugins. It gives you control to play, pause, loop, or mute audio in modern browsers.

Understand the <audio> Tag in HTML

The <audio> tag lets you add sound files to your web page. It plays music, effects, or voice tracks in supported formats. It replaces old plugin-based methods.

The <audio> tag works in all major browsers and gives you native controls or allows full control through JavaScript.

Here is the basic tag:

<audio src="sound.mp3" controls></audio>
  • <audio> is the main tag.
  • src points to the sound file.
  • controls shows a default play bar with play and pause.

These are the key attributes you can use with the <audio> Tag:

AttributeWhat it does
srcSets the path to the audio file
controlsShows the built-in play bar
autoplayStarts playing as soon as the page loads
loopPlays the file again when it ends
mutedStarts the audio with sound off
preloadTells the browser how to load the audio

Here are different formats that are supported in browsers:

FormatBest Use CaseCompatibility
MP3Common and smallWorks in all major browsers
OGGOpen formatWorks in Firefox and Chrome
WAVHigh qualityLarge size, limited use

If a browser cannot play a format, write multiple <source> tags inside <audio>.

Here is the difference between <audio> and other media tags like <video>:

  • <audio> plays sound only.
  • <video> plays both sound and picture.
  • <audio> works with smaller files.
  • <video> adds more layout rules and UI space.

Use <audio> for background music or narration, and <video> when you need visuals.

Use <source> Inside <audio>

You can include multiple audio formats inside the <audio> tag with the <source> element. That specifies the path to the audio file in different formats.

Here is an example:

<audio controls>
  <source src="sound.mp3" type="audio/mpeg">
  <source src="sound.ogg" type="audio/ogg">
  Your browser does not support audio.
</audio>

The browser plays the first file it supports, while the last line shows if none of the formats work.

Here is another example:

<audio controls>
  <source src="alert.mp3" type="audio/mpeg">
  <source src="alert.ogg" type="audio/ogg">
</audio>

This plays a sound called alert.mp3 or alert.ogg. The browser checks support and loads the first one it can play. The controls attribute adds a default play bar.

The controls attribute shows you many buttons, such as:

  • Play
  • Pause
  • Volume
  • Seek options

Here is a tag with controls attribute:

<audio src="music.mp3" controls></audio>

Browsers add a simple UI. It works on desktop and mobile.

Customize the UI of the HTML <audio> Tag with JavaScript and CSS

Use JavaScript to link buttons to audio actions and use CSS to style those buttons. But don’t forget to hide the default controls

Here’s a sample example:

<audio id="myAudio" src="track.mp3"></audio>
<button onclick="document.getElementById('myAudio').play()">Play</button>
<button onclick="document.getElementById('myAudio').pause()">Pause</button>

This gives full control without the browser UI.

Use the autoplay attribute:

<audio src="intro.mp3" autoplay></audio>

The browser starts the file once it loads. Some browsers block autoplay unless muted.

Add loop:

<audio src="loop.mp3" loop controls></audio>

The audio restarts every time it ends.

Here is an example:

<audio src="background.mp3" autoplay loop muted></audio>

This starts playing at load, plays again after each end, and starts with sound off.

How to Style the HTML <audio> Tag

You can style the size, border, or visibility of the player. For full control, hide it and use custom buttons.

Here is an example:

<audio id="customPlayer" src="clip.mp3"></audio>
<button onclick="document.getElementById('customPlayer').play()">Play</button>
<style>
  button {
    background: black;
    color: white;
    padding: 5px 10px;
    border: none;
  }
</style>

This removes the default bar and replaces it with a styled play button.

Examples of the <audio> Tag in HTML

Simple Player with Multiple Sources

<audio controls>
  <source src="theme.mp3" type="audio/mpeg">
  <source src="theme.ogg" type="audio/ogg">
  This browser does not support audio.
</audio>

This uses two formats. The browser loads one that it can play. The user sees a simple play bar.

Autoplay and Loop Background Audio

<audio src="ambient.mp3" autoplay loop muted></audio>

This runs a muted sound in the background. It loops without showing controls. It works best for background sound effects.

Custom Audio Buttons with JavaScript:

<audio id="voice" src="speech.mp3"></audio>
<button onclick="document.getElementById('voice').play()">Play</button>
<button onclick="document.getElementById('voice').pause()">Pause</button>

This setup gives full control. It links buttons to the play and pause actions. It hides the default player.

Styled Controls with CSS:

<audio id="alert" src="notice.mp3"></audio>
<button onclick="document.getElementById('alert').play()">Play Alert</button>
<style>
  button {
    background-color: red;
    color: white;
    font-size: 16px;
  }
</style>

This shows how to style the button. It gives a clear design and connects to audio playback.

Wrapping Up

In this article, you learned how the <audio> tag works and how to control it with attributes and code.

Here is a quick recap:

  • The <audio> tag adds sound to web pages.
  • Use src, controls, autoplay, loop, and muted to control playback.
  • Add multiple <source> tags for fallback.
  • Customize the UI with JavaScript and CSS.
  • Use autoplay and loop for background audio.
  • Style your player to fit your page layout.

FAQs

What is the purpose of the <audio> tag in HTML?

The <audio> tag allows you to embed sound files directly into a web page. It supports multiple formats and provides native controls for play, pause, and volume.
<audio src="sound.mp3" controls></audio>
This example creates a basic audio player with default browser controls.

How do you add multiple audio sources using the <audio> tag?

To ensure compatibility across all browsers, use the <source> tag inside <audio>. This lets the browser choose the first supported file.

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

What are the most common attributes of the <audio> tag?

Here are commonly used attributes:
  • src: Path to the audio file
  • controls: Shows browser’s built-in controls
  • autoplay: Plays audio automatically
  • loop: Repeats audio after it ends
  • muted: Mutes audio by default
Example:
<audio src="clip.mp3" controls autoplay loop></audio>

Which audio formats are supported by the <audio> tag?

The <audio> tag supports formats like:
  • MP3 - Widely supported
  • OGG - Good for open-source compatibility
  • WAV - High quality, larger size
To ensure broad support, include multiple formats using <source>.

<audio controls>
  <source src="sound.mp3" type="audio/mpeg">
  <source src="sound.ogg" type="audio/ogg">
</audio>

Similar Reads

HTML Span Tag: Inline Container Explained

The HTML span tag acts as a simple container for inline text or other inline elements. It groups text and…

HTML details Tag: How to Toggle Hidden Content

The <details> tag hides content and shows it when needed. It helps organize long pages. The tag shows extra info…

HTML iframe Tag: How to Embed Content with Examples

The iframe tag embeds another HTML page inside the current page. It loads content from a separate source. Understand the…

HTML main Tag: How to Create a Primary Content Area

The <main> tag in HTML marks the central content of a webpage. It tells browsers and assistive technologies where the…

HTML bdo Tag – Control Text Direction

Text on a page often moves from left to right. Some languages move from right to left. HTML gives control…

HTML link Tag: Linking Stylesheets

The link tag in HTML connects a web page to external resources like stylesheets or icons. It's placed in the…

HTML Deprecated Tags and Modern Alternatives List

HTML tags that no longer work stay in the deprecated list. This article explains them and shows the modern replacements.…

HTML svg Tag: Scalable Vector Graphics for the Web

The <svg> tag in HTML helps you draw graphics that scale cleanly at any size. You can animate shapes and…

HTML Attributes with Examples for Beginners

HTML attributes add extra detail to elements so the browser knows how to handle them. Here you will see examples…

Comments in HTML: How They Work with Examples

Comments in HTML help you explain your code. You can add notes without showing anything in the browser. Understand How…

Previous Article

HTML hr Tag: Horizontal Rules

Next Article

HTML video Tag: How to Embed Video in a Web Page

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *


Subscribe to Get Updates

Get the latest updates on Coding, Database, and Algorithms straight to your inbox.
No spam. Unsubscribe anytime.