I’ve built a custom HTML5 audio player using jQUery according to the guide found here: http://neutroncreations.com/blog/building-a-custom-html5-audio-player-with-jquery/
My script is as follows:
jQuery(document).ready(function() {
audio = jQuery('div#artificial-brothers audio').get(0);
loadingIndicator = jQuery('div#artificial-brothers #loading');
positionIndicator = jQuery('div#artificial-brothers #handle');
timeleft = jQuery('div#artificial-brothers #timeleft');
if ((audio.buffered != undefined) && (audio.buffered.length != 0)) {
jQuery(audio).bind('progress', function() {
var loaded = parseInt(((audio.buffered.end(0) / audio.duration) * 100), 10);
loadingIndicator.css({width: loaded + '%'});
});
} else {
loadingIndicator.remove();
}
jQuery(audio).bind('timeupdate', function() {
var rem = parseInt(audio.duration - audio.currentTime, 10),
pos = (audio.currentTime / audio.duration) * 100,
mins = Math.floor(rem/60,10),
secs = rem - mins*60;
timeleft.text('-' + mins + ':' + (secs < 10 ? '0' + secs : secs));
//if (!manualSeek) {
positionIndicator.css({width: pos + '%'});
// }
//if (!loaded) {
// loaded = true;
jQuery('div#artificial-brothers #gutter').slider({
value: 0,
step: 0.01,
orientation: "horizontal",
range: "min",
max: audio.duration,
animate: true,
slide: function() {
manualSeek = true;
},
stop:function(e,ui) {
manualSeek = false;
audio.currentTime = ui.value;
}
});
}).bind('play',function(){
jQuery('div#artificial-brothers #playtoggle').addClass('playing');
}).bind('pause ended', function() {
jQuery('div#artificial-brothers #playtoggle').removeClass('playing');
});
jQuery('div#artificial-brothers #playtoggle').click(function() {
if (audio.paused) { audio.play(); }
else { audio.pause(); }
});
jQuery('div#artificial-brothers #stoptoggle').click(function() {
if (audio.play) { audio.pause(); }
audio.currentTime = 0;
});
});
My problem is that I need to run multiple instances of said player on the same page and I can’t seem to achieve this. I’ve tried copy/pasting the script and changing the id (artificial-brothers), but then only the script written lastly will actually work. Any ideas on how to call the player more than once on a page would be great!
//Kasper
EDIT: According to the info given by @charlieftl, my code now looks like this:
jQuery(document).ready(function() {
jQuery('.player').each(function(){
var container = jQuery(this);
var audio = container.find('audio').get(0);
var loadingIndicator = container.find('.loading');
var positionIndicator = container.find('.handle');
var slider = container.find('.gutter');
var timeleft = container.find('.timeleft');
if ((audio.buffered != undefined) && (audio.buffered.length != 0)) {
jQuery(audio).bind('progress', function() {
var loaded = parseInt(((audio.buffered.end(0) / audio.duration) * 100), 10);
loadingIndicator.css({width: loaded + '%'});
});
} else {
loadingIndicator.remove();
}
jQuery(audio).bind('timeupdate', function() {
var rem = parseInt(audio.duration - audio.currentTime, 10),
pos = (audio.currentTime / audio.duration) * 100,
mins = Math.floor(rem/60,10),
secs = rem - mins*60;
timeleft.text('-' + mins + ':' + (secs < 10 ? '0' + secs : secs));
//if (!manualSeek) {
positionIndicator.css({width: pos + '%'});
// }
//if (!loaded) {
// loaded = true;
slider.slider({
value: 0,
step: 0.01,
orientation: "horizontal",
range: "min",
max: audio.duration,
animate: true,
slide: function() {
manualSeek = true;
},
stop:function(e,ui) {
manualSeek = false;
audio.currentTime = ui.value;
}
});
});
container.find('.playtoggle').click(function() {
if (audio.paused) { audio.play(); }
else { audio.pause(); }
});
container.find('.stoptoggle').click(function() {
if (audio.play) { audio.pause(); }
audio.currentTime = 0;
});
jQuery(audio).bind('play',function(){
container.find('.playtoggle').addClass('playing');
});
jQuery(audio).bind('pause ended', function() {
container.find('.playtoggle').removeClass('playing');
});
});
});
Judging by your use of jQuery selectors I suspect you are repeating element ID’s in your page. ID’s must be unique so most of your selectors should only reflect the actual ID of element. WHen an element has an ID, there is never any reason to start the selector at a higher level … the ID itself is the most efficient selector available
Eaxmple from your code:
Should be:
To get around your issue you need to change repeating ID’s to a class instead. Wrap each of the html instances for your audio in a container with a common class
Now you can loop over all of these instances using
$.each. Within the loop you always search only for elements within that instance.Following is not a complete rewrite of all of your code but is enough of an example to set a pattern that will allow you to keep instances insulated from each other