MDN self-hosted library · e01.org

Cross-browser audio basics

Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.

html
<audio controls>
  <source src="audio-file.mp3" type="audio/mpeg" />
  <source src="audio-file.ogg" type="audio/ogg" />
  <!-- fallback for non-supporting browsers goes here -->
  <p>
    Your browser does not support HTML audio, but you can still
    <a href="audio-file.mp3">download the music</a>.
  </p>
</audio>
html
<audio autoplay>…</audio>
html
<audio loop>…</audio>
html
<audio muted>…</audio>
html
<audio preload="auto">…</audio>
html
<audio controls>…</audio>
html
<audio src="audio-file.mp3">…</audio>
html
<audio src="audio-file.mp3" type="audio/mpeg">…</audio>
html
<audio id="my-audio" src="audio-file.mp3">…</audio>
js
const audio = document.getElementById("my-audio");
js
const audio = document.createElement("audio");

if (audio.canPlayType("audio/mpeg")) {
  audio.setAttribute("src", "audio-file.mp3");
}

if (audio.canPlayType("audio/ogg")) {
  audio.setAttribute("src", "audio-file.ogg");
}

alert("play");

audio.play();

alert("stop");

audio.pause();

alert("play from 5 seconds in");

audio.currentTime = 5;
audio.play();
js
audio.play();
js
audio.pause();
js
if (audio.canPlayType("audio/mpeg")) {
  // It's supported.
  // Do something here!
}
js
if (audio.currentTime > 5) {
  audio.currentTime = 3;
}
js
// set the volume at 50%
audio.volume = 0.5;
html
<audio id="my-audio">
  <source src="audio-file.mp3" type="audio/mpeg" />
  <source src="audio-file.ogg" type="audio/ogg" />
  <!-- place fallback here as <audio> supporting browsers will ignore it -->
  <p>Download<a href="audio-file.mp3">audio-file.mp3</a></p>
</audio>

<!-- custom play and pause buttons -->
<button id="play">play</button>
<button id="pause">pause</button>
js
const audio = document.getElementById("my-audio");
const play = document.getElementById("play");
const pause = document.getElementById("pause");

// associate functions with the 'onclick' events
play.onclick = playAudio;
pause.onclick = pauseAudio;

function playAudio() {
  audio.play();
}

function pauseAudio() {
  audio.pause();
}
js
audio.addEventListener("loadstart", () => {
  // Grabbing the file
});
js
audio.addEventListener("durationchange", () => {
  // You can display the duration now
});
js
audio.addEventListener("loadedmetadata", () => {
  // You can display the duration now
});
js
audio.addEventListener("loadeddata", () => {
  // You could display the playhead now
});
js
audio.addEventListener("progress", () => {
  // you could let the user know the media is downloading
});
js
audio.addEventListener("canplay", () => {
  // Audio is ready to play
});
js
audio.addEventListener("canplaythrough", () => {
  // Audio is ready to play all the way through
});
js
audio.addEventListener("timeupdate", () => {
  // Update something related to playback progress
});
js
audio.addEventListener("ended", () => {
  // Do something once audio track has finished playing
});
html
<audio id="my-audio">
  <source
    src="http://jPlayer.org/audio/mp3/Miaow-07-Bubble.mp3"
    type="audio/mpeg" />
  <source
    src="http://jPlayer.org/audio/ogg/Miaow-07-Bubble.ogg"
    type="audio/ogg" />
  <!-- place fallback here as <audio> supporting browsers will ignore it -->
  <a href="audio-file.mp3">audio-file.mp3</a>
</audio>

<div id="controls">
  <span id="loading">loading</span>
  <button id="play">play</button>
  <button id="pause">pause</button>
</div>
<div id="progress">
  <div id="bar"></div>
</div>
css
#controls {
  width: 80px;
  float: left;
}

#progress {
  margin-left: 80px;
  border: 1px solid black;
}

#bar {
  height: 20px;
  background-color: green;
  width: 0;
}

#play,
#pause {
  display: none; /* hide until media is ready */
}
js
const audio = document.getElementById("my-audio");
const play = document.getElementById("play");
const pause = document.getElementById("pause");
const loading = document.getElementById("loading");
const bar = document.getElementById("bar");

function displayControls() {
  loading.style.display = "none";
  play.style.display = "block";
}

// Check that the media is ready before displaying the controls
if (audio.paused) {
  displayControls();
} else {
  // not ready yet - wait for canplay event
  audio.addEventListener("canplay", () => {
    displayControls();
  });
}

play.addEventListener("click", () => {
  audio.play();
  play.style.display = "none";
  pause.style.display = "block";
});

pause.addEventListener("click", () => {
  audio.pause();
  pause.style.display = "none";
  play.style.display = "block";
});

// Display progress
audio.addEventListener("timeupdate", () => {
  // Sets the percentage
  bar.style.width = `${Math.floor(
    (audio.currentTime / audio.duration) * 100,
  )}%`;
});
css
#progress {
  margin-left: 80px;
  border: 1px solid black;
  cursor: pointer;
}
js
const progress = document.getElementById("progress");

progress.addEventListener("click", (e) => {
  // Calculate the normalized position clicked
  const clickPosition = (e.pageX - progress.offsetLeft) / progress.offsetWidth;
  const clickTime = clickPosition * audio.duration;

  // Move the playhead to the correct position
  audio.currentTime = clickTime;
});
js
bufferedTimeRanges = audio.buffered;
js
seekableTimeRanges = audio.seekable;