html audio tag | html audio autoplay - codewithrandom

html audio tag | html audio autoplay - codewithrandom

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:


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 / 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


<audio src="URL" controls ></audio>


Attributes


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.

<audio src="file.mp3" autoplay></audio>


Note: Mobile browsers do not allow autoplay


controls


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


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


<audio src="file.mp3" controls autoplay muted></audio>


loop


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>


preload


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


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>.


<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> 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.


written by -

Post a Comment

Previous Post Next Post