DEV Community

Codewithrandom Blogs
Codewithrandom Blogs

Posted on

Html Audio Autoplay Tag - Make Audio Autoplay in HTML

The 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 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 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, is widely supported among all modern browsers. However, Internet Explorer 8 and earlier do not support the element.

Html Audio Tag / Basic Syntax:

<audio src="URL" />
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

Audio Attributes in HTML

The HTML 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>
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

The Boolean attribute muted mutes the audio file upon initial play.

<audio src="file.mp3" controls autoplay muted></audio>
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

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 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 . Next, nest child elements inside .

<audio controls>
  <source src="file.ogg" type="audio/ogg" />
  <source src="file.mp3" type="audio/mp3" />
</audio>
Enter fullscreen mode Exit fullscreen mode

Fallbacks for Old Browsers

Those browsers that do not support the tag displays the text in between the tag.

<audio src="file.mp3" controls>Audio tag not supported</audio>
Enter fullscreen mode Exit fullscreen mode

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 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>
Enter fullscreen mode Exit fullscreen mode

In this article, we covered everything you need to know to get started with audio 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 element.

Thanks For Reading This Article.

Top comments (1)

Collapse
 
sumonajana321 profile image
Sumona Jana

This is such an insightful post! I really appreciate how you broke down the topic in a way that's easy to understand. Looking forward to implementing some of these tips in my daily routine!
Boarding schools in Hyderabad