You can consider this tutorial as a consequence of my earlier html5 audio API tutorial. In this tutorial, we will see how to control the audio with jQuery. This is handy if you are about to develop a dynamic application that makes use of several audio media and wants to control them from your jQuery script efficiently. Let’s start rolling!
How Far We Can Control?
Well, we can control almost every feature that defaults HTML5 player provides, like play/pause/volume up/down, mute on/off etc and additional functionality like stop, forward/backward capability etc.
So, if you have N number of audio files on a single page, and instead of letting users deal with N audio players, you can give it an efficient interface and control what to play/stop etc, with jQuery.
Checkout The jQuery Audio Controller Demo
The Audio Media:
Let’s use the following code as our HTML5 code for the audio media:
We have kept two media because of compatibility issues on browsers, so the alternative one is being loaded.
Loading The Audio With jQuery:
as you can see on the above HTML5 code, we have set the “preload” option to “none,” which means no information about the audio file will be loaded on the page load, neither the audio file nor any metadata. We will do them by ourselves from jQuery to boost the page load performance. Let’s use the following code to load the audio:
We can also add an event listener to know when it’s loaded. However, the event will be triggered immediately after the metadata is loaded and the audio starts loading. But it won’t wait till the audio loads in full, which usually loads on demand. To do something after the loading, use the following code before triggering the load event:
Start playing audio and pause. It is fairly easy to handle; you just need to trigger corresponding events as below:
However, there is no ready event to stop audio, so we will need to do it with the help of ‘pause’ event and another property named “currentTime,” which indicates the current playing time. Here is what we will do to stop:
The Default HTML5 player doesn’t provide this facility, but we can easily make such functionality with a little jQuery code. Here is a small example code to do so:
The audio player has its own “volume,” properly which can be controlled with jQuery as below:
We can instantly make audio off and turn it on to its earlier volume level easily with the use of “muted” property. See the example code below, which toggles the current mute state:
I hope this small tutorial on controlling HTML5 audio with jQuery will help you develop audio/music-driven web applications easily. If you are having any issues running any example or with the demo, please let me know by commenting here. Happy coding 🙂
I’m using the latest releases of Firefox and Chrome on Ubuntu 12.04. For me, your demo works fine in Firefox but not at all in Chrome. Have you noticed this?
Md Ali Ahsan Rana says
I use mac, not ubuntu. But as being *nix based, should have been behaving in same way. However, thanks for noticing and reporting. I will check. Thanks.
Not sure what the problem was. I downloaded your script, invoked the initialisation differently and all works fine across Chrome, Opera and Firefox. Very useful – thanks.
C. Uy says
Md Ali Ahsan Rana says
is there any option to only show muted control, please reply
nice one , helped me a lot , thanks 🙂
I’m using a HTML5 player in joomla that’s control by jquery. It keeps looping the song. Is there a way to stop the repeat?
Hi there, is there a way to exclude or deactivate the volume control from the standard html5 audio control.
im using multiple audio tags
i want it to be played one at a time
this is the code
aoa… dear how to play songs using this tag if we store path of song file in database
joe hart says
Hi, good explication. In this moment I have a problem, I not know control the preload for created a progress bar of preload of audio.
Fabio Andres Pino G says
How refresh the tag of audio?, cuz already im supported, 3 diferent source, when are available, so already i add the new source with append (jqeury) but, some times the audio appear desactive and others times work, when appear desactive, just reload the page, and work….. if somebady can help me i would apprentice it, and thaks for the future responde
Mike S says
I have a list of tracks that I would like to play over and over randomly until I click a stop button. Any clues on how to set that up?
I have sound track for every page. Want to turn off the audio when turn next page. Also, the audio on the earlier page must start from the beginning if I visit that page again. How should code. I have put html5 audio player.
Thank you very much.
If you trigger the .load() method for that player, it should reset the audio.
I am creating Rss Feeds [https://mySocialFeeds.com/] which I am importing into the audio app to play different songs / playlists. Works really well!