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 noscript Tag: How to Display Fallback When JS Is Disabled

The HTML noscript tag tells the browser what to show when JavaScript does not run. This tag targets users who…

HTML Form Tag: How Create User Input Forms

The HTML form tag helps you collect user input on a website. You use the HTML form tag to create…

HTML Document Structure: How to Create DOCTYPE in HTML

The DOCTYPE ensures that your HTML is readable by browsers and ensures that pages follow modern standards. Understand the DOCTYPE…

HTML b Tag: How to Bold Text

Use the HTML b tag to make text bold without giving it extra meaning. It adds visual weight only. Understand…

HTML s Tag: How it Works with Examples

The HTML s tag places a line across text that has no longer value or does not hold importance. It…

HTML figure Tag: Responsive Design & Media Elements

The HTML figure tag links media with a caption. Use it to group an image, diagram, code block, or video…

HTML hr Tag: Horizontal Rules

The <hr> tag in HTML gives you a fast way to split content with a clean horizontal line. You may…

HTML title Tag: How to Set Web Page Titles

The title tag in HTML shows the page name in the browser tab and helps search engines know the topic.…

HTML meta Tags for SEO

In this article, you will learn what HTML meta tags are and how they work. You also see how they…

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…

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.