How do you make an audio file play in html?


The HTML element is used to play an audio file on a web page.


The HTML

To play an audio file in HTML, use the element:

Example

Try it Yourself »


HTML Audio - How It Works

The controls attribute adds audio controls, like play, pause, and volume.

The element allows you to specify alternative audio files which the browser may choose from. The browser will use the first recognized format.

The text between the and tags will only be displayed in browsers that do not support the element.


HTML

To start an audio file automatically, use the autoplay attribute:

Example

Try it Yourself »

Note: Chromium browsers do not allow autoplay in most cases. However, muted autoplay is always allowed.

Add muted after autoplay to let your audio file start playing automatically (but muted):

Example

Try it Yourself »


Browser Support

The numbers in the table specify the first browser version that fully supports the element.

Element
4.0 9.0 3.5 4.0 10.5


HTML Audio Formats

There are three supported audio formats: MP3, WAV, and OGG. The browser support for the different formats is: 

BrowserMP3WAVOGG
Edge/IE YES YES* YES*
Chrome YES YES YES
Firefox YES YES YES
Safari YES YES NO
Opera YES YES YES

*From Edge 79


HTML Audio - Media Types

File FormatMedia Type
MP3 audio/mpeg
OGG audio/ogg
WAV audio/wav

HTML Audio - Methods, Properties, and Events

The HTML DOM defines methods, properties, and events for the element.

This allows you to load, play, and pause audios, as well as set duration and volume.

There are also DOM events that can notify you when an audio begins to play, is paused, etc.

For a full DOM reference, go to our HTML Audio/Video DOM Reference.


HTML Audio Tags

TagDescription
Defines sound content
Defines multiple media resources for media elements, such as


❮ Audio Object

Example

An audio player with play and pause buttons:

var x = document.getElementById("myAudio");

function playAudio() {
  x.play();
}

function pauseAudio() {
  x.pause();
}

Try it Yourself »


Definition and Usage

The play() method starts playing the current audio.

Tip: This method is often used together with the pause() method.

Tip: Use the controls property to display audio controls (like play, pause, seeking, volume, etc, attached on the audio).


Browser Support

Method
play() Yes 9.0 Yes Yes Yes

Syntax

Parameters

Return Value


❮ Audio Object



Example

Play a sound file:

Try it Yourself »


Definition and Usage

The tag is used to embed sound content in a document, such as music or other audio streams.

The tag contains one or more tags with different audio sources. The browser will choose the first source it supports.

The text between the and tags will only be displayed in browsers that do not support the element.

There are three supported audio formats in HTML: MP3, WAV, and OGG.

Audio Format and Browser Support

BrowserMP3WAVOGG
Edge / IE YES YES* YES*
Chrome YES YES YES
Firefox YES YES YES
Safari YES YES NO
Opera YES YES YES

*From Edge 79


Tips and Notes

Tip: For video files, look at the tag.



Browser Support

The numbers in the table specify the first browser version that fully supports the element.

Element
4.0 9.0 3.5 4.0 11.5

Attributes

AttributeValueDescription
autoplay autoplay Specifies that the audio will start playing as soon as it is ready
controls controls Specifies that audio controls should be displayed (such as a play/pause button etc)
loop loop Specifies that the audio will start over again, every time it is finished
muted muted Specifies that the audio output should be muted
preload auto
metadata
none
Specifies if and how the author thinks the audio should be loaded when the page loads
src URL Specifies the URL of the audio file

Global Attributes

The tag also supports the Global Attributes in HTML.


Event Attributes

The tag also supports the Event Attributes in HTML.


HTML DOM reference: HTML Audio/Video DOM Reference


Default CSS Settings

None.



Can I play audio in HTML?

The HTML .

How do I autoplay audio in HTML?

The simplest way to automatically play content is to add the autoplay attribute to your .

How do I connect audio in HTML?

You can also use the tag or the newer . With audio files, we recommend using the . MP3 file format because of its wide acceptance on the Internet, and is utilized by all browsers and operating systems.

How do I add an MP3 player to my HTML?

The HTML