- Home
- /
- Tutorials
- /
- HTML Tutorial
- /
- HTML Audio
HTML Images and Media
HTML Audio
The audio element puts a sound player on your page with one line: audio controls plus a file. Play, pause, volume and the timeline come from the browser for free, no plugin needed.
HTML <audio> Syntax
Audio Example
html
<audio controls>
<source src="https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3" type="audio/mpeg">
Your browser does not support the audio element.
</audio><audio>is the player.controlsshows the play button, timeline and volume. Without it, the player is invisible.<source>points to the file. You can list several, and the browser plays the first one it supports.- The text inside is shown only in very old browsers that do not know
<audio>.
Attributes
| Attribute | What it does |
|---|---|
| controls | Shows the browser's player controls. |
| autoplay | Tries to start playing when the page loads. Usually blocked, see below. |
| loop | Starts again from the beginning when it ends. |
| muted | Starts with the sound off. |
| preload | How much to download before play is pressed: none, metadata (just the length) or auto. |
HTML Audio Attributes
html
<audio controls autoplay loop>
<source src="https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3" type="audio/mpeg">
Your browser does not support the audio element.
</audio>Why autoplay does not work
Browsers block sound that starts by itself, because it annoys and startles people. Chrome, Safari and Firefox only allow autoplay when the audio is muted, or after the user has interacted with the site. So autoplay on its own usually does nothing. Let the reader press play.
Which file format?
- MP3 plays in every current browser. If you provide one file, make it MP3.
- AAC (.m4a) also plays everywhere and sounds better at the same size.
- OGG and Opus are open formats with excellent quality, supported everywhere except some older Apple devices.
- WAV is uncompressed. It is huge, so avoid it on the web.
Listing an OGG or Opus file first and an MP3 second is a common pattern: browsers that support the smaller file use it, and the rest fall back to MP3.
Example: Complete HTML Audio Player
Complete HTML Audio Example
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>HTML Audio Example</title>
</head>
<body>
<h2>Listen to Our Audio Track</h2>
<audio controls>
<source src="https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3" type="audio/mpeg">
Your browser does not support the audio element.
</audio>
<p>Enjoy the music directly from your browser without any plugins!</p>
</body>
</html>A player with the standard controls appears under the heading. Nothing plays until the reader presses the button.
Browser Support
Chrome | Edge | Firefox | Safari | Opera | IE9+ |
|---|---|---|---|---|---|
| Yes | Yes | Yes | Yes | Yes | Partially |
Good to know
- Add a transcript for spoken audio, such as a podcast. It helps deaf readers and search engines, which cannot listen.
- For background music, think twice: most readers will mute it or leave.
- To control the player from JavaScript, use
play(),pause()and theendedevent. For video, the same ideas apply in HTML Video.
HTML Audio exercises
Add an audio player
Add an audio player that plays this file and shows its play button and volume: https://ik.imagekit.io/html5andcss3/lesson-samples/audio.mp3
Result
