Html Audio Autoplay Tag - Make Audio Autoplay in HTML
Published: 2023-06-16 04:04:14

**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](https://www.codewithrandom.com/2022/12/17/html-audio-tag-html-audio-autoplay-codewithrandom/)** 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.

There are 3 file formats supported by <audio> element:

File Format	MIME Type	Remarks
.mp3	audio/mp3	Supported by all modern browsers
.wav	audio/wav	Not supported by Internet Explorer
.ogg	 
audio/ogg	Not supported by Internet Explorer and Safari
Html Audio Tag

As of 2021, <audio> is widely supported among all modern browsers. However, Internet Explorer 8 and earlier do not support the <audio> element.

## Html Audio Tag / Basic Syntax:
```
<audio src="URL" />
```
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 Tag
```
<audio src="URL" controls ></audio>
```
**Audio Attributes in HTML**

The HTML <audio> tag supports Global attributes and Event attributes, as well as a few more other attributes such as:

## Html Audio Autoplay Attribute

using "autoplay" attribute In Audio Tag your Audio Autoplay.
```
<audio src="file.mp3" autoplay></audio>
```
This is a Boolean attribute that indicates whether the audio file should be played automatically as soon as it is loaded.

Note: Mobile browsers do not allow autoplay

## Html Audio Controls Tag

The boolean attribute controls display the audio controls on the webpage, such as play and pause buttons.
```
<audio src="file.mp3" controls autoplay></audio>
Muted Audio HTML Attribute
```
The Boolean attribute muted mutes the audio file upon initial play.
```
<audio src="file.mp3" controls autoplay muted></audio>
```
## Audio Autoplay Loop Html

Using Loop attribute with autoplay in audio tag your Audio Autoplay On Loop. You can try this Code to Autoplay On Loop Song.
```
<audio src="file.mp3" controls autoplay loop></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.

**Preload Audio HTML Attribute**

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 
```
<audio src="file.mp3" controls preload = "auto"></audio>
```
**Type Audio HTML Attribute**

The attribute type specifies the MIME type of audio file. It is very useful while adding multiple sources of the audio file.

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>.
```
<audio controls>
  <source src="file.ogg" type="audio/ogg" />
  <source src="file.mp3" type="audio/mp3" />
</audio>
```
## Fallbacks for Old Browsers

Those browsers that do not support the <audio> tag displays the text in between the <audio> tag.
```
<audio src="file.mp3" controls>Audio tag not supported</audio>
```
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.
```
<audio controls>
  <source src="file.ogg" type="audio/ogg" />
  <source src="file.mp3" type="audio/mp3" />
  Audio tag not supported. Here is
     a <a href="file.mp3">link to the audio</a> instead.
</audio>
```
In this article, we covered everything you need to know to get started with **[audio](https://www.codewithrandom.com/2022/12/17/html-audio-tag-html-audio-autoplay-codewithrandom/)** tag in HTML. There are much more things that we will learn when we will use CSS and Javascript to add style and functionality to the <audio> element.

Thanks For Reading This Article.