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.

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

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

Mobile browser support

Chrome
18+
Firefox
4+
Opera
All
Safari
All
Basics
Introduction
Syntax
Editors
Basic Examples
Head Section
<!DOCTYPE>
Tags and Elements
Semantic Elements
Tags Reference
Attributes
Comments
Block and Inline Elements
Forms
Form Elements
Input
Responsive Web Design
Inline Scripts
Uniform Resource Locator
Redirect
XHTML
Geolocation
Drag and Drop
Local Storage
Web Workers
Server-Sent Events
Character Encoding
Text Formatting
Quotation and Citation Elements
Headings
Paragraphs
Links
Tables
Lists
Symbols
Space
Tab
Styles
Computer Code
Layout
Classes
Colors
Images
iframes
Audio Player
Video Player
YouTube Videos
Multimedia
Canvas
SVG
<!-- -->
<a>
<abbr>
<acronym> DEPRECATED
<address>
<applet> DEPRECATED
<article>
<aside>
<audio>
<b>
<base>
<basefont> DEPRECATED
<bdi>
<bdo>
<big> DEPRECATED
<blink> DEPRECATED
<blockquote>
<body>
<br>
<button>
<canvas>
<caption>
<center> DEPRECATED
<cite>
<code>
<col>
<colgroup>
<datalist>
<dd>
<del>
<details>
<dfn>
<dialog>
<dir> DEPRECATED
<div>
<dl>
<dt>
<em>
<embed>
<fieldset>
<figcaption>
<figure>
<font> DEPRECATED
<footer>
<form>
<frame> DEPRECATED
<frameset> DEPRECATED
<h1> – <h6>
<head>
<header>
<hr>
<html>
<i>
<iframe>
<img>
<input>
<ins>
<kbd>
<keygen> DEPRECATED
<label>
<legend>
<li>
<link>
<main>
<map>
<mark>
<menu>
<menuitem> DEPRECATED
<meta>
<meter>
<nav>
<noframes> DEPRECATED
<noscript>
<object>
<ol>
<optgroup>
<option>
<output>
<p>
<param>
<pre>
<progress>
<q>
<rp>
<rt>
<ruby>
<s>
<samp>
<script>
<section>
<select>
<small>
<source>
<span>
<strike> DEPRECATED
<strong>
<style>
<sub>
<summary>
<sup>
<table>
<tbody>
<td>
<tfoot>
<th>
<thead>
<time>
<title>
<tr>
<track>
<tt> DEPRECATED
<u>
<ul>
<var>
<video>
<wbr>