HOME C C++ PYTHON JAVA HTML CSS JAVASCRIPT BOOTSTRAP JQUERY REACT PHP SQL AJAX JSON DATA SCIENCE AI

HTML Audio

HTML Audio is a feature of the Hypertext Markup Language (HTML) that allows web developers to embed audio content directly into a web page. It provides a way to include audio files, such as music or sound effects, so that visitors to a website can listen to them without needing to download separate files or use external media players.

To embed audio in an HTML document, you can use the

Example
 <audio controls>
   <source src= "example.mp3" type= "audio/mp3">
  Your browser does not support the audio element.
 </audio>

  • The <audio> element is the container for the audio content.
  • The controls attribute adds play, pause, and volume control buttons to the audio player.
  • The <source> element inside <audio> specifies the source file of the audio and its type. You can include multiple <source> elements with different file formats to ensure compatibility with various browsers.

The text "Your browser does not support the audio element" serves as a fallback message for browsers that do not support the <audio> element.

You can use different audio formats like MP3, Ogg, or WAV, and the browser will automatically choose the supported format. The actual file (example.mp3 in this case) should be replaced with the path to your audio file.

Additionally, you can customize the appearance and behavior of the audio player using CSS and JavaScript if needed.

HTML <audio> Autoplay

To start an audio file automatically, use the autoplay attribute:

Example

 <audio autoplay>
    <source src= "horse.ogg" type= "audio/ogg">
   <source src= "horse.mp3" type= "audio/mpeg">
Your browser does not support the audio element.
 </audio>
You can click on above box to edit the code and run again.

Output


Add muted after autoplay to let your audio file start playing automatically (but muted)

Example

<audio autoplay muted>
   <source src="horse.ogg" type="audio/ogg">
  <source src="horse.mp3" type="audio/mpeg">
Your browser does not support the audio element.
</audio>
You can click on above box to edit the code and run again.

Output

HTML Audio Formats

There are three supported audio formats: MP3, WAV, and OGG. The browser support for the different formats is:

Browser MP3 WAV OGG
Chrome Yes YES* YES*
Firefox YES YES YES
Safari YES YES No
Opera YES YES YES

HTML Audio - Media Types

File Format Media Type
MP3 audio/mpeg
OGG audio/ogg
WAV audio/wav