Subscribe

Vanilla JavaScript playing Audio

โœ๏ธ

See how easy it is to play Audio with Vanilla JavaScript

12 Apr, 2020 ยท 4 min read

For me, audio and video are elements you would normally go and look for a library. But actually, itโ€™s really easy playing audio in Vanilla JavaScript. Let me guide you through playing a song in the browser with nothing but Vanilla JavaScript.

Playing Audio with Vanilla JavaScript

For the HTML part we use the following markup:

<a
  href="https://freesound.org/data/previews/511/511919_919187-lq.mp3"
  class="js--music-player"
  >โ–ถ๏ธ Play</a
>
<br />
<a
  href="https://freesound.org/data/previews/511/511749_9271295-lq.mp3"
  class="js--music-player"
  >โ–ถ๏ธ Play</a
>

In this case we choose to use a js--music-player class so we can experiment with doing multiple players on one page.

We could also have chosen to go with a data-attribute selector, read more about that in this article.

For the JavaScript part, we use the following code which we will go through step by step.

// Loop through all elements with class js--music-player
Array.from(document.querySelectorAll('.js--music-player')).forEach(function (
  player
) {
  // We initially set a Audio element with the contents of our href
  player.audio = new Audio(player.href);

  // Add click event to this element
  player.addEventListener('click', function (event) {
    // Check if this player is playing ๐Ÿ˜Ž
    if (event.target.classList.contains('playing')) {
      // Playing so pause our audio track
      event.target.audio.pause();
      // Remove our playing class
      event.target.classList.remove('playing');
      // Set the content to play again
      event.target.innerText = 'โ–ถ๏ธ Play';
    } else {
      // Not playing so we can start playing
      event.target.audio.play();
      // Add the class of playing
      event.target.classList.add('playing');
      // Change text to Stop
      event.target.innerText = '๐Ÿ›‘ Stop';
    }

    // Prevent from going to the actual link
    event.preventDefault();
  });
});

Letโ€™s walk through each item in detail

// Loop through all elements with class js--music-player
Array.from(document.querySelectorAll('.js--music-player')).forEach(function (
  player
) {});

As you can see we are using document.querySelectorAll(".js--music-player") as our selector to get all elements on our page that have the class js--music-player. We pass these elements inside a native Array.from...forEach function. This function will create an array of our elements and loop through them.

The only action we do in this loop is the following:

// We initially set a Audio element with the contents of our href
player.audio = new Audio(player.href);

The player.audio() line will set a new Audio element with the content of our playerโ€™s URL (mp3).

Next, we add a click listener to our element with the following code:

// Add click event to this element
player.addEventListener('click', function (event) {});

Since we are already looping over our elements, we might as well use this opportunity to add this listener now, instead of doing another eventListener.

Inside the click we do two important parts

// Check if this player is playing ๐Ÿ˜Ž
if (event.target.classList.contains('playing')) {
  // Playing so pause our audio track
} else {
  // Not playing so we can start playing
}

// Prevent from going to the actual link
event.preventDefault();

First, we check if the playerโ€™s classList contains the class playing we check this to maintain a track of which one is playing or not.

Then we run event.preventDefault(); to stop the link from actually going to the file.

classList has multiple options like add/remove/contains. Read more about it in this article.

If our player does not have the class of playing we need to make sure it starts playing:

// Not playing so we can start playing
event.target.audio.play();
// Add the class of playing
event.target.classList.add('playing');
// Change text to Stop
event.target.innerText = '๐Ÿ›‘ Stop';

We set the audio (we set in the initial loop) to play(). You will now hear the music play.

Then we add the class we where initially looking for of playing, and then we change the text of our player to contain Stop.

So if we click this link again, it will now have the class of playing and we run the following code:

// Playing so pause our audio track
event.target.audio.pause();
// Remove our playing class
event.target.classList.remove('playing');
// Set the content to play again
event.target.innerText = 'โ–ถ๏ธ Play';

This pauses the audio from playing, will remove the playing class from the element and change the text back to what it was before.

You can see this in action here:

See the Pen Vanilla JavaScript playing Audio by Chris Bongers (@rebelchris) on CodePen.

Thank you for reading, and letโ€™s connect!

Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter

Spread the knowledge with fellow developers on Twitter
Tweet this tip
Powered by Webmentions - Learn more

Read next ๐Ÿ“–

Removing all vowels with JavaScript

3 Dec, 2022 ยท 2 min read

Removing all vowels with JavaScript

10 games to learn JavaScript

2 Dec, 2022 ยท 3 min read

10 games to learn JavaScript

Join 2097 devs and subscribe to my newsletter