How to use audio in HTML5

The audio tag is a brand new HTML5 element (it not existed in XHTML) which can be used to incorporate music in a website. The big benefit of this audio tag is you are no longer limited to midi music incorporation, like in the previous HTML version, but you can also add MP3 and Ogg files to a website. Although this might sound like music to your ears you need to take into account MP3 and Ogg files are not always browser compatible and supported. On the one hand browsers based on Webkit (Chrome & Safari) do support regular MP3 files, but on the other hand these same MP3 files will need to be converted to Ogg files if you would like other browsers like Firefox to play the audio files.

Example:

<audio controls>
<source src="audio-file.ogg" type="audio/ogg">
<source src="audio-file.mp3" type="audio/mpeg">
Your browser does not support the audio tag.
</audio>


Was this useful?