I’m using the WordPress playlist shortcode to present a few audio samples on a given page. For example:
The shortcode, by default loops the playlist. The documentation on the codex doesn’t show an option to disable autorepeat/looping of the playlist via a parameter.
I need to disable the looping of the playlist; that is play it only once. I’ve tried binding to the ‘ended’ event as suggested on this post but I’m not sure if this older post is still valid. The example code won’t fire at all.
Any suggestions on how to acheive this?
1 Answer
I was able to bind to the audio
object’s ended
event. But there is certainly more logic that would need to be added. This is working for me:
(function($){
var customAudio = {
initialize: function() {
var self = this;
// execute logic independently for each playlist in the document
$('.wp-playlist').each(function(index) {
// determine the last track in the playlist so that
// when the last track ends we can pause the audio object
lastTrack = $(this).find('.wp-playlist-tracks .wp-playlist-item:last a');
// bind to the audio object's ended event and execute
// our own ended logic
$(this).find('audio').on('ended', function() {
// pass the audio object and the last track
self.ended(this, lastTrack);
});
});
},
ended : function (audio, lastTrack) {
// if this audio element is the last track, pause it
if ( audio.currentSrc === lastTrack.attr('href') ) {
audio.pause();
}
}
};
$(function() {
customAudio.initialize();
});
}(jQuery));