Before HTML5 arrived on the scene, there was no standard for playing audio files on a web page. Therefore, the only way to play audio files was to use a plug-in such as flash. Now, the HTML5
Using the
Let’s begin by taking a look at a typical
<audio controls="controls" id="audio_player">
<source src="track.ogg" type="audio/ogg" />
<source src="track.mp3" type="audio/mpeg" />
Your browser does not support the audio element.
</audio>
Nested within the
The tag also supports a few special attributes that control the playback of the audio:
autoplay – This can be set to “true” or left blank (“”) to define whether the track should automatically play as soon as the page has loaded.
controls – As seen in the example above, this defines whether the native controls such as ‘play, pause’ etc should be shown.
loop – This can be set to “loop” and defines whether the track should play again once it has finished.
preload – This can be set to “auto” (which describes whether the file should load as soon as the page loads), “metadata” (which describes whether only the metadata, track title etc. should be loaded), “none” (which dictates that the browser should not load the file when the page loads).
src – The URL of the video to embed. This is optional as the element may be placed within the block to specify the file.
Notice how the appearance of the audio control can vary wildly! The first is the Chrome version, while the second is what you get in Internet Explorer:
Controlling Playback via the HTMLAudioElement Interface
The HTMLMediaElement JavaScript interface exposes the properties and methods required to support basic media-related capabilities that are common to both audio and video. Hence, the HTMLVideoElement and HTMLAudioElement elements both inherit this interface.
The HTMLMediaElement interface is meant to be utilized in conjunction with your own HTML player in such a way that mimics the element so that you can give it the exact appearance that you want. Behind the scenes, the element is still what’s playing the audio, but it should be made invisible. The easiest way to do that is to remove the controls attribute. We can then present the following player:
You’ll notice in the HTML markup above that the play button’s onclick event triggers our playAudio() function. In it, we can check whether or not the player is currently paused. If it is, we call the audio element’s play() method; otherwise, we call pause(). There is also some CSS class toggling done in order to change the button:
// variable to store HTML5 audio elementvar music = document.getElementById('audio_player');
functionplayAudio() {
if (music.paused) {
music.play();
pButton.className = "";
pButton.className = "pause";
} else {
music.pause();
pButton.className = "";
pButton.className = "play";
}
}
Setting the Volume Programmatically
Much in the same way that the play button’s onclick event triggers our playAudio() function, the range element’s onchange event causes the setVolume() function to execute. It simply sets the audio element’s volume property to that of the range element, which was declared to go from 0 to one with 0.1 increments:
In today’s tutorial we learned how to build a simple audio player using nothing but HTML, CSS, and JavaScript. In the next instalment, we’ll add some more advanced functionality, such as moving the current point in the song via the playhead and iterating over a playlist.
The original home of HTML tutorials. HTMLGoodies is a website dedicated to publishing tutorials that cover every aspect of being a web developer. We cover programming and web development tutorials on languages and technologies such as HTML, JavaScript, and CSS. In addition, our articles cover web frameworks like Angular and React.JS, as well as popular Content Management Systems (CMS) that include WordPress, Drupal, and Joomla. Website development platforms like Shopify, Squarespace, and Wix are also featured. Topics related to solid web design and Internet Marketing also find a home on HTMLGoodies, as we discuss UX/UI Design, Search Engine Optimization (SEO), and web dev best practices.
Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.