Build a Custom HTML Music Player, Using JavaScript and the Web Audio API

This tutorial will show you how to build a custom music player, using the Web audio API, that is uniquely branded with CSS, HTML, and JavaScript. 

HTML5 and the Web Audio API are tools that allow you to own a given website’s audio playback experience.

Rather than always using the browser defaults or third-party solutions to play audio, we can tap into the supplied free APIs and deliver a more branded design to end users on the web.

With that in mind, let’s leverage JavaScript to use the Web Audio API and ultimately give the end user more control over their audio listening experience.

Our HTML Audio Player Demo

Click on the play button, listen to it go.

The final audio player features volume, play, pause, and scrubbing controls. We’ll also make it look sleek with some rich color and design, loosely based on some of the designs in this After Effects Music Player Pack on Envato Elements.

For the purposes of this tutorial our audio player is simple. It can, however, be redesigned and extended to use a track list should you need more than one audio file at a given time. 

Playback in CodePen and Google Chrome

Whilst building this audio player, I noticed an issue with the CodePen demo in Google Chrome. If you play the audio track, then refresh the page, you’ll probably find the track won’t play again. It seems the combination of CodePen’s iframes, plus Google’s Autoplay policy, conspire to give us a low media engagement score and render the track unplayable.

As you’ll see, we add the recommended code to allow for playing audio based on user interaction (a click) but we’re still getting errors in this case. Rest assured that once outside the CodePen environment our audio player works just fine in Google Chrome.

1. The HTML

Let’s begin by adding the necessary HTML to make this concept a reality! We need to have an <audio> tag element on the page to tap into the Web Audio API most efficiently. This doesn’t need to display visually by default, but it’s crucial that we have it on the page.

Linked inside the src attribute of the <audio> tag on the page, you’ll find an MP3 audio track.

info
I used “Outfoxing the Fox” by Kevin MacLeod, released this under Creative Commons. I borrowed it from the MDN example Boombox project.

The core player HTML markup comprises a variety of containers, controls, and SVG icons. We leverage a range input to adjust volume and swap between a play and pause icon for better use of space.

2. Styling the Player

Your music player can be as unique as you like. I chose my own preferred style, which is quite simplistic and will allow you to add and remove your own details without too much trouble. Here’s the final CSS I ended up with.

In most cases, I maintained a naming convention for class names with the prefix .player- to keep things modular. A utility class of .hidden was added, which will come in handy when we tackle the JavaScript portion of this tutorial.

3. Making it All Work With JavaScript

To tap into the Web Audio API, we need some form of media element in the HTML. You can optionally create one with JavaScript, but I find it a little easier to render something on the page that’s not in view.

You’ll recall we added the <audio> tag in a previous step. We’ll query for it first and also create something known as a new AudioContext() instance.

What’s AudioContext?

AudioContext is an interface that represents an audio-processing graph built from audio modules that are linked together (AudioNodes).

That’s a complicated way of saying that an AudioContext is a grouping of audio nodes you can interact with and manipulate. Things like volume (gain), panning, and more are options available to tweak. We’ll interact more with this feature coming up when we address volume controls.

The track variable allows us to adjust the audio element’s properties directly using the AudioContext. Read more about the createMediaElementSource() method.

“An AudioContext is a grouping of audio nodes you can interact with and manipulate.”

Querying for Player Controls

What’s a custom player without custom controls? The next step is to query for all the custom controls we added in the HTML. We’ll using the Document.querySelector() method to return each associated element assigned by a variable.

Here we have variables for each independent control and the progress bar shown in the user interface.

Waiting Before JavaScript Fires

To properly load the audio, which can sometimes take longer than other items on the page, it probably makes sense to wait for the entire page to load before we run any JavaScript.

We’ll start with an event listener that waits for the page to load. We can wrap the entirety of our code in this block.

We’ll start by listening for the playButton variable’s click event to instruct our player to Play.

A few things happen at once when the playButton gets clicked.

  1. Browsers are smart enough to stop auto-playing audio from playing on the first load. Inside the AudioContext method, there is a state method that returns a value of “suspended”, “running”, or “closed”. In our case, we’ll be looking for “suspended”. If that‘s the state that returns, we can proceed to resume the audio with the method called resume().
  2. We use data attributes in the HTML to denote when the button is “playing” or “paused”.

  3. If the play or pause button is clicked, we can dynamically tell the audioElement to play or pause.

  4. For a better user experience, I added the ability to show and hide the play or pause icons depending on the player’s state.

Update Time Stamps and Progress

Each track you load with an AudioElement context will have its characteristics and metadata you can display in the HTML. We start by making everything zero on the first-page load and proceed to call a function that dynamically updates and formats the time as the audio gets played or paused.

We’ll additionally show a progress bar that will dynamically fill based on the amount of lapsed audio. This is handy for the end user who might want to glance at a progress bar rather than read the remaining time.

I created two functions that are extracted elsewhere in the JavaScript file. The main thing to denote about the code above is the type of event listener we keep track of. The timeupdate event is unique to media like Audio or Video within the Web API.

Displaying and Formatting Time 

We can use the playerCurrentTime and playerDuration variables to display and format time. We’ll set the textContent of those tags in the HTML to match a new timestamp relative to the audioElement’s current attributes. An audioElement will have a currentTime property and a duration property.

Using the Date API in JavaScript, we can tap into a handy one-liner to convert the default seconds that get returned from currentTime and duration in a format that matches HH:MM:SS (Hours, Minutes, Seconds).

Updating Player Progress

Updating the progress bar in our HTML is relatively simple and comes down to a percentage calculation. We’ll get the percent returned by dividing the audioElement.currentTime by the audioElement.duration and multiplying that by 100.

Finally, we can set some CSS via JavaScript by using the progressFilled variable we created before and adjusting the flex-basis property to grow or shrink depending on the change percentage.

 Add Volume Controls

Adjusting volume taps back into the AudioContext object we used before. We’ll need to call a method named createGain() and change the gain value to map to the volume range input within the HTML.

We created a track variable early on in this tutorial and are finally putting it to use here. Using the connect() method, you can connect the track to the gainNode and then to the AudioContext. Without this line, the volume range input doesn’t know about the volume of the audio.

We’ll listen for a change event to map the volume relative to the gain.

What Happens When the Audio Ends?

We can reset the player after the audio ends so it can be ready for another listen should the end user want to start it over.

Here we toggle the play button icon from pause to play, set the data-playing attribute to false, reset the progress bar, and the audioElement’s currentTime and duration properties.

Scrubbing the Progress Bar to Skip and Rewind

Our progress bar is functional visually, but it would be more helpful if you could click anywhere on the timeline and adjust the current audio playback. We can achieve this with a series of event listeners and a new function.

The scrub() function requires an event argument we listen for. In particular, the offsetX property allows us to pinpoint where a user clicked and make calculations relative to the audioElement’s properties.

Finally, we can listen on the progress bar itself for a set of events like click, mousemove, mousedown, and mouseup to adjust the audio element’s currentTime property.

4. Putting it All Together

The final JavaScript code is below. One thing to note is on the first-page load; I call the setTimes() function once again so we can get real-time displayed correctly before the user even starts manipulating the audio player.

Conclusion

There you have it! With a bit of JavaScript and elbow grease, you can create your very own branded music player.

From here, you might experiment with adding more controls, like skipping buttons or panning buttons. I’d also check out the AudioTracklist interface, which allows you to create playlists and extend the design as necessary.


This content originally appeared on Envato Tuts+ Tutorials and was authored by Andy Leverenz

This tutorial will show you how to build a custom music player, using the Web audio API, that is uniquely branded with CSS, HTML, and JavaScript. 

HTML5 and the Web Audio API are tools that allow you to own a given website’s audio playback experience.

Rather than always using the browser defaults or third-party solutions to play audio, we can tap into the supplied free APIs and deliver a more branded design to end users on the web.

With that in mind, let’s leverage JavaScript to use the Web Audio API and ultimately give the end user more control over their audio listening experience.

Our HTML Audio Player Demo

Click on the play button, listen to it go.

The final audio player features volume, play, pause, and scrubbing controls. We’ll also make it look sleek with some rich color and design, loosely based on some of the designs in this After Effects Music Player Pack on Envato Elements.

For the purposes of this tutorial our audio player is simple. It can, however, be redesigned and extended to use a track list should you need more than one audio file at a given time. 

Playback in CodePen and Google Chrome

Whilst building this audio player, I noticed an issue with the CodePen demo in Google Chrome. If you play the audio track, then refresh the page, you’ll probably find the track won’t play again. It seems the combination of CodePen’s iframes, plus Google’s Autoplay policy, conspire to give us a low media engagement score and render the track unplayable.

As you’ll see, we add the recommended code to allow for playing audio based on user interaction (a click) but we’re still getting errors in this case. Rest assured that once outside the CodePen environment our audio player works just fine in Google Chrome.

1. The HTML

Let’s begin by adding the necessary HTML to make this concept a reality! We need to have an <audio> tag element on the page to tap into the Web Audio API most efficiently. This doesn’t need to display visually by default, but it’s crucial that we have it on the page.

Linked inside the src attribute of the <audio> tag on the page, you’ll find an MP3 audio track.

info
I used “Outfoxing the Fox” by Kevin MacLeod, released this under Creative Commons. I borrowed it from the MDN example Boombox project.

The core player HTML markup comprises a variety of containers, controls, and SVG icons. We leverage a range input to adjust volume and swap between a play and pause icon for better use of space.

2. Styling the Player

Your music player can be as unique as you like. I chose my own preferred style, which is quite simplistic and will allow you to add and remove your own details without too much trouble. Here’s the final CSS I ended up with.

In most cases, I maintained a naming convention for class names with the prefix .player- to keep things modular. A utility class of .hidden was added, which will come in handy when we tackle the JavaScript portion of this tutorial.

3. Making it All Work With JavaScript

To tap into the Web Audio API, we need some form of media element in the HTML. You can optionally create one with JavaScript, but I find it a little easier to render something on the page that’s not in view.

You’ll recall we added the <audio> tag in a previous step. We’ll query for it first and also create something known as a new AudioContext() instance.

What’s AudioContext?

AudioContext is an interface that represents an audio-processing graph built from audio modules that are linked together (AudioNodes).

That’s a complicated way of saying that an AudioContext is a grouping of audio nodes you can interact with and manipulate. Things like volume (gain), panning, and more are options available to tweak. We'll interact more with this feature coming up when we address volume controls.

The track variable allows us to adjust the audio element's properties directly using the AudioContext. Read more about the createMediaElementSource() method.

“An AudioContext is a grouping of audio nodes you can interact with and manipulate.”

Querying for Player Controls

What’s a custom player without custom controls? The next step is to query for all the custom controls we added in the HTML. We’ll using the Document.querySelector() method to return each associated element assigned by a variable.

Here we have variables for each independent control and the progress bar shown in the user interface.

Waiting Before JavaScript Fires

To properly load the audio, which can sometimes take longer than other items on the page, it probably makes sense to wait for the entire page to load before we run any JavaScript.

We’ll start with an event listener that waits for the page to load. We can wrap the entirety of our code in this block.

We’ll start by listening for the playButton variable’s click event to instruct our player to Play.

A few things happen at once when the playButton gets clicked.

  1. Browsers are smart enough to stop auto-playing audio from playing on the first load. Inside the AudioContext method, there is a state method that returns a value of “suspended”, “running”, or “closed”. In our case, we’ll be looking for “suspended”. If that‘s the state that returns, we can proceed to resume the audio with the method called resume().
  2. We use data attributes in the HTML to denote when the button is “playing” or “paused”.

  3. If the play or pause button is clicked, we can dynamically tell the audioElement to play or pause.

  4. For a better user experience, I added the ability to show and hide the play or pause icons depending on the player’s state.

Update Time Stamps and Progress

Each track you load with an AudioElement context will have its characteristics and metadata you can display in the HTML. We start by making everything zero on the first-page load and proceed to call a function that dynamically updates and formats the time as the audio gets played or paused.

We’ll additionally show a progress bar that will dynamically fill based on the amount of lapsed audio. This is handy for the end user who might want to glance at a progress bar rather than read the remaining time.

I created two functions that are extracted elsewhere in the JavaScript file. The main thing to denote about the code above is the type of event listener we keep track of. The timeupdate event is unique to media like Audio or Video within the Web API.

Displaying and Formatting Time 

We can use the playerCurrentTime and playerDuration variables to display and format time. We’ll set the textContent of those tags in the HTML to match a new timestamp relative to the audioElement’s current attributes. An audioElement will have a currentTime property and a duration property.

Using the Date API in JavaScript, we can tap into a handy one-liner to convert the default seconds that get returned from currentTime and duration in a format that matches HH:MM:SS (Hours, Minutes, Seconds).

Updating Player Progress

Updating the progress bar in our HTML is relatively simple and comes down to a percentage calculation. We’ll get the percent returned by dividing the audioElement.currentTime by the audioElement.duration and multiplying that by 100.

Finally, we can set some CSS via JavaScript by using the progressFilled variable we created before and adjusting the flex-basis property to grow or shrink depending on the change percentage.

 Add Volume Controls

Adjusting volume taps back into the AudioContext object we used before. We’ll need to call a method named createGain() and change the gain value to map to the volume range input within the HTML.

We created a track variable early on in this tutorial and are finally putting it to use here. Using the connect() method, you can connect the track to the gainNode and then to the AudioContext. Without this line, the volume range input doesn’t know about the volume of the audio.

We’ll listen for a change event to map the volume relative to the gain.

What Happens When the Audio Ends?

We can reset the player after the audio ends so it can be ready for another listen should the end user want to start it over.

Here we toggle the play button icon from pause to play, set the data-playing attribute to false, reset the progress bar, and the audioElement’s currentTime and duration properties.

Scrubbing the Progress Bar to Skip and Rewind

Our progress bar is functional visually, but it would be more helpful if you could click anywhere on the timeline and adjust the current audio playback. We can achieve this with a series of event listeners and a new function.

The scrub() function requires an event argument we listen for. In particular, the offsetX property allows us to pinpoint where a user clicked and make calculations relative to the audioElement’s properties.

Finally, we can listen on the progress bar itself for a set of events like click, mousemove, mousedown, and mouseup to adjust the audio element’s currentTime property.

4. Putting it All Together

The final JavaScript code is below. One thing to note is on the first-page load; I call the setTimes() function once again so we can get real-time displayed correctly before the user even starts manipulating the audio player.

Conclusion

There you have it! With a bit of JavaScript and elbow grease, you can create your very own branded music player.

From here, you might experiment with adding more controls, like skipping buttons or panning buttons. I’d also check out the AudioTracklist interface, which allows you to create playlists and extend the design as necessary.


This content originally appeared on Envato Tuts+ Tutorials and was authored by Andy Leverenz


Print Share Comment Cite Upload Translate Updates
APA

Andy Leverenz | Sciencx (2022-09-26T15:04:03+00:00) Build a Custom HTML Music Player, Using JavaScript and the Web Audio API. Retrieved from https://www.scien.cx/2022/09/26/build-a-custom-html-music-player-using-javascript-and-the-web-audio-api/

MLA
" » Build a Custom HTML Music Player, Using JavaScript and the Web Audio API." Andy Leverenz | Sciencx - Monday September 26, 2022, https://www.scien.cx/2022/09/26/build-a-custom-html-music-player-using-javascript-and-the-web-audio-api/
HARVARD
Andy Leverenz | Sciencx Monday September 26, 2022 » Build a Custom HTML Music Player, Using JavaScript and the Web Audio API., viewed ,<https://www.scien.cx/2022/09/26/build-a-custom-html-music-player-using-javascript-and-the-web-audio-api/>
VANCOUVER
Andy Leverenz | Sciencx - » Build a Custom HTML Music Player, Using JavaScript and the Web Audio API. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/09/26/build-a-custom-html-music-player-using-javascript-and-the-web-audio-api/
CHICAGO
" » Build a Custom HTML Music Player, Using JavaScript and the Web Audio API." Andy Leverenz | Sciencx - Accessed . https://www.scien.cx/2022/09/26/build-a-custom-html-music-player-using-javascript-and-the-web-audio-api/
IEEE
" » Build a Custom HTML Music Player, Using JavaScript and the Web Audio API." Andy Leverenz | Sciencx [Online]. Available: https://www.scien.cx/2022/09/26/build-a-custom-html-music-player-using-javascript-and-the-web-audio-api/. [Accessed: ]
rf:citation
» Build a Custom HTML Music Player, Using JavaScript and the Web Audio API | Andy Leverenz | Sciencx | https://www.scien.cx/2022/09/26/build-a-custom-html-music-player-using-javascript-and-the-web-audio-api/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.