Thanks to the HTML5
audio element, gone are the days of having to use Flash to include audio files on a web page. In this brief tutorial we’ll demonstrate how to add audio to your HTML using the new tag.
This is how you add audio to HTML5 using the
<audio controls loop autoplay preload="none" src="deep-house-track.mp3"> </audio>
There are a few attributes you can use with the audio element:
controlsadds controls for play/pause, volume and seeking
loopwill repeat the playback
autoplayplays the audio automatically
preloadcan take the values none, metadata, or auto. The default is auto. It can be good to set it to none on pages where you include multiple
audioelements, preventing the browser from downloading all the audio data.
You may want to provide multiple formats because MP3 is not an open format, therefore some browsers can’t support it. A free format, Ogg, would be great across the board, but it’s not supported by all browsers. See here for a list of browser format support. Use the
source element within the
audio element to provide different formats:
<audio> <source src="deep-house-track.ogg"> <source src="deep-house-track.mp3"> </audio>
And you can be even more exhaustive by providing a
<audio> <source src="deep-house-track.ogg" type="audio/ogg"> <source src="deep-house-track.mp3" type="audio/mp3"> </audio>
And you can provide extra markup for browsers that don’t support
<audio> <source src="deep-house-track.ogg"> <source src="deep-house-track.mp3"> <p>Your browser doesn't support this, <a href="…">download the file</a>.</p> </audio>
This is the basic functionality of the new
<audio> element in HTML5.
<audio> element is a significant new feature of HTML. To check for browser support, you can use the Browser Support lookup for
<audio> on the Can I Use website
Or, for a more comprehensive review of the HTML5 markup language, explore our series, How To Build a Website in HTML.
Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in our Questions & Answers section, find tutorials and tools that will help you grow as a developer and scale your project or business, and subscribe to topics of interest.Sign up
Click below to sign up and get $100 of credit to try our products over 60 days!