HTML5 | Audio

Since the release of HTML5, audios can be added to webpages using the “audio” tag. Previously audios could be only played on webpages using web plugins like Flash. The “audio” tag is an inline element which is used to embed sound files into a web page. It is a very useful tag if you want to add audio such as songs, interviews, etc on your webpage.

Syntax:

<audio>
  <source src="sample.mp3" type="audio/mpeg">
</audio>

Attributes :
The various attributes that can be used with the “audio” tag are listed below :

  1. Controls : Designates what controls to display with the audio player.
  2. Autoplay : Designates that the audio file will play immediately after it loads controls.
  3. Loop : Designates that the audio file should continuously repeat.
  4. src : Designates the URL of the audio file.
  5. muted : Designates that the audio file should be muted.

Supported Formats :
Three formats mp3, ogg, wav are supported by HTML5. The support for each format by different browsers is given below :

Browser MP3 WAV OGG
Google Chrome Yes Yes Yes
Internet Explorer Yes No No
Firefox Yes Yes Yes
Opera Yes Yes Yes
Safari Yes Yes No

Adding audio on Webpage :

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<body>
  
    <p>Audio Sample</p>
  
    <audio controls>
        <source src="test.mp3" type="audio/mp3">
        <source src="test.ogg" type="audio/ogg">
    </audio>
  
</body>
  
</html>

chevron_right


Output :

Code Explanation :

  • The controls attribute is used to add audio controls such as play, pause, and volume.
  • The “source” element is used to specify the audio files which the browser may use. The first recognised format is used by the browser.

Autoplaying audio on a Webpage :
The autoplay attribute is used to automatically begin playback of the audio file whenever the URL of the webpage is loaded.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<body>
  
    <p>Audio Sample</p>
  
    <audio controls autoplay>
        <source src="test.mp3" type="audio/mp3">
        <source src="test.ogg" type="audio/ogg">
    </audio>
  
</body>
  
</html>

chevron_right


Output :

Adding muted audio file on a Webpage :
The muted attribute specifies that the audio should be muted on the webpage.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<body>
  
    <p>Audio Sample</p>
  
    <audio controls muted>
        <source src="test.mp3" type="audio/mp3">
        <source src="test.ogg" type="audio/ogg">
    </audio>
  
</body>
  
</html>

chevron_right


Output :

Adding audio using the source element :
The source element can be used to add audio files to a webpage. The src attribute is used to specify the source of the file specified.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<body>
  
    <p>Audio Sample</p>
  
    <audio controls autoplay>
        <source src="test.mp3" type="audio/mp3">
    </audio>
  
</body>
  
</html>

chevron_right


Output :

Adding audio with multiple sources :
Multiple sources of audios are specified so that if the browser is unable to play the first source then it will automatically jump to the second source and try to play it.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<body>
  
    <p>Audio Sample</p>
  
    <audio controls autoplay>
        <source src="test.mp3" type="audio/mp3">
        <source src="test.ogg" type="audio/ogg">
        <source src="test.opus" type="audio/ogg">
    </audio>
  
</body>
  
</html>

chevron_right


Output :

Adding audio using the “Embed” tag :

Adding audios on a webpage using the “embed” tag is an old technique. This method does work but is comparatively less efficient than the other methods.
The user must have a plugin like MIDI or QuickTime because the embed tag requires a plugin for support.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<body>
  
    <p>Audio Sample</p>
  
    <embed src="test.mp3" width="200" height="50" 
                     autoplay="true" loop="true">
  
</body>
  
</html>

chevron_right


Output :

Supported browser: The browser supported by <audio> tag are listed below :

  • Google Chrome 4.0
  • Internet Explorer 9.0
  • Firefox 3.5
  • Opera 10.5
  • Safari 4.0


My Personal Notes arrow_drop_up

I am a technology enthusiast who has a keen interest in programming I am pursuing Engineering in Computer Science from GEU, Dehradun I like to unwind by watching movies and English sitcomsI have a keen interest in music

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : nidhi_biet



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.