How to add audio
Point the <audio> element at a sound file and add controls:
<audio src="song.mp3" controls></audio>
controls shows the browser's player: play and pause, a timeline, the time and a volume control. Without controls the element takes no space at all. The preview cannot load files from your computer, so this example builds a short WAV file in the browser with a script and uses it as the src. Press play:
The player looks different in Chrome, Firefox and Safari, because each browser draws its own. Delete controls and the player disappears, though the audio is still there for JavaScript to play.
Audio attributes
| Attribute | What it does |
|---|---|
controls | Shows the browser's player |
autoplay | Tries to start playing on load (blocked with sound, see below) |
loop | Starts again when the file ends |
muted | Starts with the volume off |
preload | none (download nothing until play), metadata (length only) or auto (browser decides) |
src | The file to play; or use <source> children for several formats |
Use preload="none" or "metadata" for pages with many players, such as a podcast episode list, so the page does not download every file up front.
Formats and fallbacks
List several files with <source> and the browser plays the first one it supports. Text inside the element shows only in browsers without <audio> support:
<audio controls preload="metadata">
<source src="episode.opus" type="audio/ogg; codecs=opus">
<source src="episode.mp3" type="audio/mpeg">
<p>Your browser cannot play audio. <a href="episode.mp3">Download the episode</a>.</p>
</audio>
| Format | MIME type | Notes |
|---|---|---|
| MP3 | audio/mpeg | Plays in every current browser; the safe single choice |
AAC (.m4a) | audio/mp4 | Widely supported, good quality at small sizes |
Opus or Vorbis (.ogg, .webm) | audio/ogg, audio/webm | Small files; list MP3 as a fallback, because Safari support varies by version |
| WAV | audio/wav | Uncompressed and large; fine for short sound effects |
Why autoplay fails
Browsers do not let a page start making sound on its own. autoplay, or calling play() before the user has clicked or tapped, is refused. This example tries to play on load and prints what happened, then offers a button:
You will usually see NotAllowedError, the error a browser gives when it blocks playback. The button works because a click counts as permission. Each browser has its own extra rules (Chrome, for example, remembers sites where you often play media), so never rely on autoplay: start sound from a user action, and always handle the promise that play() returns.
new Audio(url) creates an audio element in JavaScript without adding it to the page, which is handy for sound effects.
Build your own controls
Hide the built-in player by leaving out controls, then drive the element with its JavaScript API: play(), pause(), currentTime, duration, volume, and events such as timeupdate and ended:
The button is a real <button>, so it works with the keyboard, and its label changes between Play and Pause. At the end of the file the browser pauses the element and fires ended, so the button returns to Play by itself. If you build custom controls, you also take over what the built-in ones gave you for free: keyboard support, labels and a volume control.
Accessibility
- Never start sound by itself. It interrupts screen readers and startles people. Start audio from a click.
- Offer a transcript for podcasts and spoken audio. A link to a text version under the player is enough, and search engines can read it.
- Keep
controls, or build controls from real buttons, so the audio can be paused with the keyboard.
For video with sound, captions and posters, see the video tag page; <audio> shares most of its attributes and all of its JavaScript API.
Common mistakes
- Forgetting
controls. The element is invisible without it, and it looks like nothing happened. - Expecting
autoplayto work. Start playback from a click and handle the promise fromplay(). - A wrong file path or type. Open the file URL directly to check it, and match
typeto the real format (audio/mpegfor MP3). - Loading every file on a long list. Use
preload="none"or"metadata". - Background music that cannot be stopped. Always give people a visible way to pause.
Frequently Asked Questions
How do I add audio to an HTML page?
Use <audio src="song.mp3" controls></audio>. The controls attribute shows the browser's player with a play button, timeline and volume. Without it, the element is invisible.
Why does my audio not autoplay?
Browsers block audio that starts with sound before the user has interacted with the page. Start playback from a click with audio.play() instead. A muted element may autoplay, but for audio that is rarely useful.
Which audio format should I use in HTML?
MP3 plays in every current browser, so it is the safe choice for a single file. AAC in an .m4a file is also widely supported. Use <source> elements to offer more than one format.
How do I loop audio in HTML?
Add the loop attribute: <audio src="rain.mp3" controls loop>. When the file ends, it starts again from the beginning.
How do I play a sound with JavaScript?
Create or select an audio element and call play(), for example new Audio('click.mp3').play() inside a click handler. play() returns a promise that rejects if the browser blocks playback.
Can I style the HTML audio player?
Only a little: you can set its width, margin and display, but the buttons inside are drawn by the browser and look different in each one. For a custom look, hide the controls and build your own with buttons and the audio JavaScript API.