html audio tag | html audio autoplay – codewithrandom
The <audio> tag in HTML
With the release of the new HTML5, audios can now be added directly to your HTML webpage using the “audio” tag. Before HTML5, audio could only be played on websites using web plugins such as Flash Player.
The <audio> element can stream audio directly from your browser, you can stream the audio file using a microphone via getUserMedia() or from an audio source using the src attribute.
As of 2021, <audio> is widely supported among all modern browsers. However, Internet Explorer 8 and earlier do not support the <audio> element.
There are 3 file formats supported by <audio> element:
html audio tag / Basic Syntax:
The src embeds the specific audio file into the page, it takes the file location as its value.
By default, the audio element does not show any controls to the audio element. Therefore the audio will only play if it is set to autoplay.
Instead, use the controls attribute to show the audio control panel to the user, and thus you do not have to autoplay the audio file.
HTML audio controls
The HTML <audio> tag supports Global attributes and Event attributes, as well as a few more other attributes such as:
HTML audio autoplay
This is a Boolean attribute that indicates whether the audio file should be played automatically as soon as it is load.
Note: Mobile browsers do not allow autoplay
The boolean attribute controls display the audio controls on the webpage, such as play and pause buttons.
<audio src=“file.mp3” controls autoplay></audio>
The Boolean attribute muted mutes the audio file upon initial play.
<audio src=“file.mp3” controls autoplay muted></audio>
The Boolean attribute loop, tells the browser to play the audio on loop. A song that will start over again, every time it is finished. If absent, the audio file stops when finished.
<audio src=“file.mp3” controls autoplay loop></audio>
It specifies how the and when audio file should be loaded on the page when the page loads.
It takes the following values:
auto – It loads the entire audio file when the page loads.
metadata – It only loads the metadata of the file when the page loads
none – It stops the browser to load any audio data when the page loads
If you don’t set autoplay, the browsers will only download the audio metadata (e.g. file length) but will not download the audio itself. To force preloading of the file you can use
The attribute type specifies the MIME type of audio file. It is very useful while adding multiple sources of the audio file.
Adding Multiple file sources
The best way to coerce the browser to play audio (or video) is to use the <source> element. The browser will try to load the first audio source, and in case it fails or the browser doesn’t support the file format, it will move on to the next audio source.
To declare multiple audio files, first remove the src attribute from <audio>. Next, nest child <source> elements inside <audio>.
Fallbacks for Old Browsers
Those browsers that do not support the <audio> tag displays the text in between the <audio> tag.
If you are using multiple file sources you can still give the not supported message to the user in case their browser doesn’t support <audio> tag.
written by –