Adding Audio to a HTML Webpage
Audio can enhance a user’s experience by invoking a mystic quality to a horoscope-reading application. It can give a notion of nostalgia by playing a TV theme song or add silly and playful aspects to a webpage designed for children. Done incorrectly, however, can turn an impressive flare into a rude annoyance. Below I will overview the simple steps in adding an audio element to your HTML with some notes on styling and etiquette.
- Use an <audio> tag-
An audio tag works just fine for modern day audio on webpages. It is supported on most browsers except Internet Explorer 8 as of 2020. It can play .mp3 files on all browsers, .wav (Not on Internet Explorer) and .ogg (Not on Internet Explorer and Safari.) You can add multiple <source> tags inside the <audio> tag to list multiple file formats. Such is the case if you want to play a .ogg file but are using Internet Explorer so you want to add a backup .mp3 file.
2. Attributes -
The audio tag has several attributes you can use to control the audio. The src attribute is the file where you are getting the audio from. Make sure to put the path URL there.
The controls attribute is used to display the audio player on your page.
It is not recommended to have an audio file play upon loading your page without the ability to for the user to pause or turn it down. Modern webpage development is veering away from this as the audio can be seen as an annoyance. You many also find your audio only plays upon load, sometimes. Which is not what you want. If the audio controls are there the user knows they can play it.
The loop attribute will keep the audio file playing over and over again.
For a complete list of all the audio attributes visit. https://www.w3schools.com/tags/tag_audio.asp.
3. Styling -
As of now, styling for the audio player is extremely limited. The only CSS properties you can change are width, box-shadow, border-radius and transform. However, you can implement some more style changes on the player using -webkit-media-controls as a CSS selector.
That is all you need to add audio to your HTML webpage. If you would like to dive a bit deeper please visit the links below.
HTML Audio Tag: Cheat Sheet & Real-World Examples 2020
The HTML audio tag allows the embedding of sound files into a web page. Read our in-depth guide to find out about its…