HTML Audio
On a web page, the HTML audio
element is used to play an audio file.
The HTML <audio> Element
The audio
element is used in HTML to play an audio file.
This example specifies which audio track to embed using the src
attribute on a nested source
element.
It is always useful to include the file's MIME type inside the type
attribute
Example 1: Single source file
Result of the above code is:
Example 2: Multiple source file
The following example includes multiple source
elements to play an audio file. The browser tries to load the first source element
if it is able to play it; if not it falls back to the second.
Result of the above code is:
HTML Audio - How It Works
Audio controls like playing, stop and volume are provided through the controls
property.
You can give alternative audio files from which the browser may select the source
element. The first recognised format is used by the browser.
Only in browsers which are not supporting audio
is displayed the text between audio
and /audio
.
HTML <audio> Autoplay
Use the autoplay
property to start an audio file automatically. Most Chromium browsers do not support autoplay.
Muted autoplay, on the other hand, is always permitted.
Add mute
to allow your audio file to automatically start playing (but mute):
Example
Result of the above code is:
Related Links
HTML Audio Formats
There are three supported audio formats: MP3, WAV, and OGG. The browser support for the different formats is:
Browser | MP3 | WAV | OGG |
---|---|---|---|
Edge/IE | YES | YES* | YES* |
Chrome | YES | YES | YES |
Firefox | YES | YES | YES |
Safari | YES | YES | NO |
Opera | YES | YES | YES |
HTML Audio - Media Types
File Format | Media Type |
---|---|
MP3 | audio/mpeg |
OGG | audio/ogg |
WAV | audio/wav |
Related Links
HTML Audio - Methods, Properties, and Events
The HTML DOM specifies the  audio
element functions, characteristics and events.
You can charge, play, and pause the audios, and set time and volume. This is also possible.
DOM events may also inform you when an audio is playing, stopped, etc.