javascript – Play an audio file using jQuery when a button is clicked

javascript – Play an audio file using jQuery when a button is clicked

Which approach?

You can play audio with <audio> tag or <object> or <embed>.
Lazy loading(load when you need it) the sound is the best approach if its size is small. You can create the audio element dynamically, when its loaded you can start it with .play() and pause it with .pause().

Things we used

We will use canplay event to detect our file is ready to be played.

There is no .stop() function for audio elements. We can only pause them. And when we want to start from the beginning of the audio file we change its .currentTime. We will use this line in our example audioElement.currentTime = 0;. To achieve .stop() function we first pause the file then reset its time.

We may want to know the length of the audio file and the current playing time. We already learnt .currentTimeabove, to learn its length we use .duration.

Example Guide

  1. When document is ready we created an audio element dynamically
  2. We set its source with the audio we want to play.
  3. We used ended event to start file again.

When the currentTime is equal to its duration audio file will stop
playing. Whenever you use play(), it will start from the beginning.

  1. We used timeupdate event to update current time whenever audio .currentTime changes.
  2. We used canplay event to update information when file is ready to be played.
  3. We created buttons to play, pause, restart.
$(document).ready(function() {
    var audioElement = document.createElement(audio);
    audioElement.addEventListener(ended, function() {;
    }, false);
        $(#length).text(Duration: + audioElement.duration +  seconds);
        $(#source).text(Source: + audioElement.src);
        $(#status).text(Status: Ready to play).css(color,green);
        $(#currentTime).text(Current second: + audioElement.currentTime);
    $(#play).click(function() {;
        $(#status).text(Status: Playing);
    $(#pause).click(function() {
        $(#status).text(Status: Paused);
    $(#restart).click(function() {
        audioElement.currentTime = 0;
<script src=></script>
    <h2>Sound Information</h2>
    <div id=length>Duration:</div>
    <div id=source>Source:</div>
    <div id=status style=color:red;>Status: Loading</div>
    <h2>Control Buttons</h2>
    <button id=play>Play</button>
    <button id=pause>Pause</button>
    <button id=restart>Restart</button>
    <h2>Playing Information</h2>
    <div id=currentTime>0</div>

It doesnt work with $(#myAudioElement).play() like you would expect. The official reason is that incorporating it into jQuery would add a play() method to every single element, which would cause unnecessary overhead. So instead you have to refer to it by its position in the array of DOM elements that youre retrieving with $(#myAudioElement), aka 0.

This quote is from a bug that was submitted about it, which was closed as feature/wontfix:

To do that wed need to add a jQuery method name for each DOM element method name. And of course that method would do nothing for non-media elements so it doesnt seem like it would be worth the extra bytes it would take.

javascript – Play an audio file using jQuery when a button is clicked

For anyone else following along, Ive simply taken Ahmets answer and updated the original askers jsfiddle here, substituting:



linking in a freely available mp3 off the web. Thank you for sharing the easy solution!

Leave a Reply

Your email address will not be published.