Skip to content

Instantly share code, notes, and snippets.

@imlucas
Created October 2, 2009 00:45
Show Gist options
  • Select an option

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

Select an option

Save imlucas/199353 to your computer and use it in GitHub Desktop.
$.player = {__items: [],__index: 0, __nowPlaying: null,__visible: true,__playing: false,__streamUrlResolver: null,__progressInterval: 0};
$.player.init = function(opts){};
//------------------------------------------------------------------//
// All events triggered by $.player (as3 style)
//------------------------------------------------------------------//
$.player.EVENTS = {
ITEMS_CHANGED : 'player:items_changed',
NOW_PLAYING_SET : 'player:now_playing_set',
SOUND_MANAGER_READY : 'player:sound_manager_ready',
PLAYLIST_COMPLETE: 'player:playlist_complete'
};
//------------------------------------------------------------------//
// Sound Manager Callbacks
//------------------------------------------------------------------//
$.player.__sm_callback_smlibrary_loaded = function(){
$.player.broadcast($.player.EVENTS.SOUND_MANAGER_READY);
};
$.player.__sm_callback_load = function(){
console.log('$.player.__sm_callback_load');
$('#time-total').html($.player.formatTime($.player.__element.duration, true));
};
$.player.__sm_callback_loading = function(){
var s = $.player.__currentSmSound;
var newWidth = $('#progress-bar').width() * (s.bytesLoaded/s.bytesTotal);
$('#progress-bar-loaded').width(newWidth + 'px');
};
$.player.__sm_callback_finish = function(){
console.log('$.player.__sm_callback_finish');
if($.player.__index == ($.player.getItems().length-1)){
console.log('playlist complete');
$.player.close();
// playlist is complete
$.player.broadcast($.player.EVENTS.PLAYLIST_COMPLETE);
}
else{
console.log('going to next');
$.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');
};
//------------------------------------------------------------------//
// Setters/Getters
//------------------------------------------------------------------//
$.player.setStreamUrlResolver = function(r){
$.player.__streamUrlResolver = r;
};
$.player.getPlaylistSongIds = function(){
return $.map($.player.getItems(), function(n,i){return n.id;});
};
//------------------------------------------------------------------//
// Is/has
//------------------------------------------------------------------//
$.player.isVisible = function(){
return $.player.__visible;
};
$.player.isPlaying = function(){
return $.player.__playing;
};
//------------------------------------------------------------------//
// Player Controls
//------------------------------------------------------------------//
$.player.close = function(){
$('#playerbox').slideUp();
$.player.__playing = true;
$.player.__visible = false;
};
$.player.open = function(){
$('#playerbox').slideDown();
$.player.__visible = true;
};
//------------------------------------------------------------------//
// Playback control
//------------------------------------------------------------------//
$.player.playSongAt = function(index){
$.player.playSong($.player.getItems()[index]);
};
$.player.next = function(){
if($.player.__index == ($.player.getItems().length-1)){
$.player.close();
// playlist is complete
$.player.broadcast($.player.EVENTS.PLAYLIST_COMPLETE);
}
else{
$.player.playSongAt($.player.__index + 1);
}
};
$.player.previous = function(){
$.player.playSongAt($.player.__index - 1);
};
$.player.resume = function(){
$.player.__element.play();
$('#play-or-pause').removeClass('play').addClass('pause');
$.player.__playing = true;
};
$.player.play = function(){
$.player.__element.play();
$('#play-or-pause').removeClass('play').addClass('pause');
$.player.__playing = true;
};
$.player.pause = function(){
$.player.__element.pause();
$('#play-or-pause').removeClass('pause').addClass('play');
$.player.__playing = false;
};
$.player.playOrPause = function(){
if($.player.__playing){
$.player.pause();
}
else{
$.player.play();
}
};
$.player.playSongInPlaylistWithId = function(id){
var index = $.inArray(id, $.player.getPlaylistSongIds());
$.player.playSong($.player.getItems()[index]);
};
$.player.playSong = function(song){
try{
clearInterval($.player.__progressInterval);
$.player.__element.pause();
}catch(e){
}
$('.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]));
// Shitty bug in safari that calls ended prematurely, so wait a little bit before attaching
setTimeout(function(){
e.addEventListener('ended',$.player.__sm_callback_finish);
}, 250);
//e.addEventListener('timeupdate', $.player.__sm_callback_timeupdate);
e.addEventListener('load', $.player.__sm_callback_load);
$.player.__progressInterval = setInterval($.player.__sm_callback_playing, 250);
e.play();
$.player.__element = e;
$.player.__playing = true;
$.player.setNowPlaying(song);
};
$.player.broadcast = function(eName, data){
$(document).trigger(eName, data);
};
$.player.setItems = function(items){
this.__items = items;
this.broadcast(this.EVENTS.ITEMS_CHANGED, this.__items);
};
$.player.getItems = function(){
return this.__items;
};
$.player.appendItem = function(item){
this.__items.push(item);
this.broadcast(this.EVENTS.ITEMS_CHANGED, this.__items);
};
$.player.clearItems = function(){
$.player.__items = [];
$.player.broadcast($.player.EVENTS.ITEMS_CHANGED, $.player.__items);
};
$.player.removeItemAt = function(index){
this.__items.splice(index, 1);
this.broadcast(this.EVENTS.ITEMS_CHANGED, this.__items);
};
/**
* Called whenever a new song starts playing.
* You should bind on $.player.EVENTS.NOW_PLAYING_SET to update your display.
*/
$.player.setNowPlaying = function(song){
$.player.__nowPlaying = song;
$.player.broadcast(this.EVENTS.NOW_PLAYING_SET, song);
};
$.player.getElement = function(){
return $.player.__element;
};
$.player.setVolume = function(level){
$.player.getElement().volume = level;
};
$.player.renderUi = function(){
$('#volume-box').hide();
$('#playlist-box').hide();
$("#volume-slider").slider({
orientation: 'vertical',
min:0,
max:100,
value:50,
animate:true,
change: function(event, ui){
$.player.setVolume(ui.value);
$('#volume-button').trigger('click');
}
});
};
$.player.renderPlaylistContents = function(){
var items = $.player.getItems();
var html = '<ul>';
if(items.length==0){
html = '<li>nothing in your queue... yet</li>';
}
else{
$.each(items, function(){
html += '<li><a class="playlist-item song-' + this.id +' album-' + this.album.id + '" rel="song-' + this.id + '">'+this.artist.name+' - '+this.title+'</a></li>';
});
}
html += '</ul>';
$('#playlist-title').html('Your Queue ('+items.length+')');
$('#playlist-contents').html(html);
$('a.playlist-item').click(function(){
var i = $(this).parents('li').prevAll().length;
console.log(i);
var songs = $.player.getItems();
$.player.playSong(songs[i]);
});
};
$.player.formatTime = function(nMSec,bAsString) {
var nSec = Math.floor(nMSec);
var min = Math.floor(nSec/60);
var sec = nSec-(min*60);
return (bAsString?(min+':'+(sec<10?'0'+sec:sec)):{'min':min,'sec':sec});
}
$.player.bindInitialEvents = function(){
$('#close-playlist-button').click($.player.hidePlaylist);
$('#playlist-button').click($.player.showPlaylist);
$('#close-button').click($.player.close);
$('#next').click($.player.next);
$('#previous').click($.player.previous);
$('#play-or-pause').click($.player.playOrPause);
$('#volume-button').toggle($.player.showVolume, $.player.hideVolume);
};
//------------------------------------------------------------------//
// Initialization
//------------------------------------------------------------------//
$(document).ready($.player.bindInitialEvents);
$(document).bind($.player.EVENTS.ITEMS_CHANGED, $.player.renderPlaylistContents);
$.player.init();
$(document).bind($.player.EVENTS.NOW_PLAYING_SET, function(e, d){
console.log('NOW PLAYING SET');
$('#now-playing').html('<img src="'+$.player.__nowPlaying.album.image.medium+'" style="float: left;margin-right: 20px;"/>'+$.player.__nowPlaying.title+' <br /> by '+$.player.__nowPlaying.artist.name+'<br />Track # '+$.player.__nowPlaying.trackNumber+' on '+$.player.__nowPlaying.album.title+'<br clear="all"/>');
});
$.player.setStreamUrlResolver(function(song){
return 'http://maxwell-vm.corp.amiestreet.com/stream/song/'+song.id+'.mp3';
});
// Yet another shitty safari bug... shit will keep on playing when the window is dumped
$(window).bind("beforeunload", function(e){
try{
$.player.__element.pause();
}
catch(e){}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment