HTML Tutorial menu

HTML Audio

The <audio> element plays sound files, such as music, a podcast or a recorded lesson.

The audio Element

Example

html
<audio src="lesson.mp3" controls></audio>

With controls, the browser shows a small player with play, a time bar and volume. Without controls, nothing is visible at all.

Several Sources

Like video, audio can list several files. The browser uses the first format it can play.

Example

html
<!DOCTYPE html>
<html>
<body>

<h2>Episode 1: Starting Freelancing</h2>

<audio controls>
  <source src="episode1.ogg" type="audio/ogg">
  <source src="episode1.mp3" type="audio/mpeg">
  Your browser cannot play this audio.
</audio>

<p>Put your own episode1.mp3 next to this page to hear it.</p>

</body>
</html>
Try it Yourself »

Audio Formats

FormatMIME typeNotes
MP3audio/mpegWorks in every browser. The safe choice
AAC / M4Aaudio/mp4Good quality at small sizes
Ogg Vorbis / Opusaudio/oggOpen formats, small files
WAVaudio/wavNo compression, very large files

Watch the MIME type: an MP3 file is audio/mpeg, not audio/mp3.

Audio Attributes

AttributeWhat it does
controlsShows the player
autoplayTries to start at once (usually blocked)
mutedStarts with no sound
loopPlays again and again
preloadnone, metadata or auto

Browsers block audio that starts by itself. Sound should only play after the user clicks something. This is good for visitors too: nobody likes a page that suddenly makes noise in a classroom or office.

Playing a Sound with a Button

You can make your own controls with JavaScript. The audio element has play(), pause(), currentTime and volume.

Example

html
<!DOCTYPE html>
<html>
<body>

<audio id="bell" src="bell.mp3"></audio>

<button onclick="ring()">Ring the bell</button>
<button onclick="stopBell()">Stop</button>
<p id="status">Ready</p>

<script>
const bell = document.getElementById("bell");

function ring() {
  bell.currentTime = 0;   // start from the beginning
  bell.volume = 0.5;      // half volume
  bell.play();
  document.getElementById("status").innerHTML = "Playing...";
}

function stopBell() {
  bell.pause();
  document.getElementById("status").innerHTML = "Stopped";
}
</script>

</body>
</html>
Try it Yourself »
Property or methodMeaning
play()Start playing
pause()Pause
currentTimeThe position in seconds (read or set)
durationThe total length in seconds
volumeFrom 0 (silent) to 1 (full)
pausedtrue if not playing

A Simple Playlist

Here three recorded lessons share one player. Each button changes the src and plays:

Example

html
<audio id="player" controls></audio>
<p>
  <button onclick="playLesson('lesson1.mp3')">Lesson 1</button>
  <button onclick="playLesson('lesson2.mp3')">Lesson 2</button>
  <button onclick="playLesson('lesson3.mp3')">Lesson 3</button>
</p>

<script>
function playLesson(file) {
  const player = document.getElementById("player");
  player.src = file;
  player.play();
}
</script>

Give Audio a Text Version

Deaf visitors cannot hear audio. For a podcast or a recorded lesson, add a transcript: the spoken words written as text under the player. A transcript also helps Google understand what the audio is about.

Example

html
<audio controls src="episode1.mp3"></audio>
<details>
  <summary>Read the transcript</summary>
  <p>Welcome to episode one. Today Ali talks about his first freelancing client...</p>
</details>

Exercise

Exercise

What is the correct type for an MP3 file inside a source tag?

Learn HTML with a teacher

Join the Web Development course at Workownix, online or in Orangi Town, Karachi, and get a certificate.

See the course Ask on WhatsApp