Skip to content

Instantly share code, notes, and snippets.

@imlucas
Created October 19, 2009 21:42
Show Gist options
  • Select an option

  • Save imlucas/213735 to your computer and use it in GitHub Desktop.

Select an option

Save imlucas/213735 to your computer and use it in GitHub Desktop.
$.player.playSong = function(song){
$('#playerbox').addClass('loading');
try{
clearInterval($.player.__progressInterval);
if($.player.getElement()){
$.player.getElement().pause();
}
}catch(e){}
if(!$.player.isVisible()){
$.player.open();
}
$('.playing').removeClass('playing');
$('.song-'+song.id).addClass('playing');
$.player.__index = $.inArray(song.id, $.player.getPlaylistSongIds());
$('#play-or-pause').removeClass('play').addClass('pause');
var e = new Audio($.player.__streamUrlResolver.apply(this, [song]));
e.autoplay = true;
// Shitty bug in safari that calls ended prematurely, so wait a little bit before attaching
setTimeout(function(){
e.addEventListener('ended',$.player.__sm_callback_finish, true);
}, 1000);
// Doesnt work in all implementations
// e.addEventListener('timeupdate', $.player.__sm_callback_timeupdate);
e.addEventListener('load', $.player.__sm_callback_load, true);
$.player.__progressInterval = setInterval($.player.__sm_callback_playing, 250);
e.load();
e.play();
$.player.__element = e;
$.player.__playing = true;
$.player.setNowPlaying(song);
$('#playerbox').removeClass('loading');
};
$.player.__sm_callback_load = function(){
console.log('$.player.__sm_callback_load');
$('#time-total').html($.player.formatTime($.player.__element.duration, true));
};
$.player.__sm_callback_finish = function(){
if($.player.__index == ($.player.getItems().length-1)){
$.player.close();
// playlist is complete
$.player.broadcast($.player.EVENTS.PLAYLIST_COMPLETE);
}
else{
$.player.next();
}
};
$.player.__sm_callback_playing = function(){
var s = $.player.__element;
// Ugh. Another shitty safari bug. timeupdate gets called on pause and shit only.
$('#time-ellapsed').html($.player.formatTime($.player.__element.currentTime, true));
$('#time-total').html($.player.formatTime($.player.__element.duration, true));
var newWidth = $('#progress-bar').width() * (s.currentTime/s.duration);
$('#progress-bar-played').width(newWidth + 'px');
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment