🚨 Get Your Free NFT Certificate Mint by Completing the Web3 Exam! START NOW

Code has been added to clipboard!

Adding an HTML5 Audio Player: Why, When and How

Reading time 3 min
Published Feb 26, 2019
Updated Oct 1, 2019

TL;DR – In HTML5, audio elements that allow playing sounds can be added to websites.

HTML5 Music Player: Introduction

In HTML4 and earlier versions, audio elements were not supported natively. This means you had to embed a special plugin (e.g., Adobe Flash or Apple Quicktime) to be able to play sound on your website. It wasn't a big deal back then, though: using a lot of dynamic content was barely possible due to slow Internet anyway.

HTML5 introduced new tags for embedding multimedia files directly to your webpage. One of them was <audio>, which allows embedding an HTML5 music player with audio controls straight into the webpage.

Syntax for HTML5 audio

To include an HTML5 audio player into your website, you will need to use a pair of <audio> tags. Notice there are some nested elements within them as well:

Example
<audio controls>
  <source src="audio-tag-example.mp3" type="audio/mpeg">
  <source src="audio-tag-sample.wav" type="audio/wav">
  <source src="audio-tag-demo.ogg" type="audio/ogg">
  Audio tag is not supported in this browser.
</audio>

Including <source> tags is necessary: using them, you specify the sources for the audio elements.

You can define multiple sources in different formats to make sure your user's browser supports at least one of them. While the HTML5 <audio> tag itself supports three formats (MP3, Wav and Ogg), the browser support for them differs.

Note: <source> represents an empty element – it has no content, only attributes.

Customizing the HTML5 Audio Player

You can modify the way your player looks and works by including one or multiple tag attributes.

DataCamp
Pros
  • Easy to use with a learn-by-doing approach
  • Offers quality content
  • Gamified in-browser coding experience
  • The price matches the quality
  • Suitable for learners ranging from beginner to advanced
Main Features
  • Free certificates of completion
  • Focused on data science skills
  • Flexible learning timetable
Udacity
Pros
  • Simplistic design (no unnecessary information)
  • High-quality courses (even the free ones)
  • Variety of features
Main Features
  • Nanodegree programs
  • Suitable for enterprises
  • Paid Certificates of completion
Udemy
Pros
  • Easy to navigate
  • No technical issues
  • Seems to care about its users
Main Features
  • Huge variety of courses
  • 30-day refund policy
  • Free certificates of completion

HTML5 Audio Controls

An uncontrollable HTML5 audio player means a terrible user experience. To provide your user with player buttons (also called the HTML5 audio controls), you need to include the controls attribute within the <audio> opening tag:

Example
<audio controls>
  <source src="audio-tag-example.mp3" type="audio/mpeg">
  Audio tag is not supported in this browser.
</audio>

Using HTML5 audio controls, the user can start and pause the playing sound, raise or lower the volume and skip to a specific part of the track using a slider.

Other Attributes

While other attributes are used less commonly, it's still good to be familiar with them in case you need to define a custom type of behavior for your HTML5 audio player.

Attribute Definition
autoplay Makes the audio start playing automatically
loop Makes the audio start playing again when it finishes
muted Makes the player muted by default
preload Instructs the browser what to preload – the whole file (auto), its metadata (metadata) or nothing at all (none)

HTML5 Audio: Useful Tips

  • The text between the <audio> tags is called the fallback content. The browser will display it if it cannot play the audio. You can add a message to the user or a direct link to the file in here.
  • Instead of nesting <source> tags, you can use the src attribute to define the source for the audio. However, it doesn't allow adding multiple sources.
  • The HTML5 <audio> is an inline element – however, it doesn't always work well for the design. You can change it to block using CSS.

Browser support

Browser image
Chrome
3+
Browser image
Edge
All
Browser image
Firefox
3.5+
Browser image
IE
9+
Browser image
Opera
10.5+
Browser image
Safari
3.1+

Mobile browser support

Browser image
Chrome
18+
Browser image
Firefox
4+
Browser image
Opera
All
Browser image
Safari
All