WordPress: how to get events from the default audio player?

I’m currently using WordPress 4.0 and all my audios on posts/pages are embedded using the default WordPress audio player – through a shortcode like:

Read More

Now, I want to track how many times the play button is pressed. So, my question is: how can I capture this player events?

PS: I tried the following (and it didn’t work):

$('.mejs-playpause-button button').click(function(){
    console.log('TESTING');
});

enter image description here

Related posts

Leave a Reply

1 comment

  1. It’s possible that the event propogation for that button is being prevented by the MediaElementJS library that manages the audio element.

    As an alternative I would suggest listening for ‘play’ events on either the MediaElementJS element or directly selecting the native HTML audio element and listening for the ‘play’ event on this object.

    I briefly attempted to get the MediaElementJS instance for a player but it is hard to get hold off because of the way that WordPress makes these instances.

    However you can add an event listener to an audio element and record the amount of times this element is fired:

      $('audio').on('play', function(){                           
            console.log("play");                                                  
       }); 
    

    This might not be the best solution if you have multiple elements on a page though.