server/apps/media/js/player.js

208 lines
6 KiB
JavaScript
Raw Normal View History

var PlayList={
urlBase:OC.linkTo('media','ajax/api.php')+'?action=play&path=',
current:-1,
items:[],
player:null,
volume:0.8,
active:false,
2011-08-12 10:34:02 +00:00
tempPlaylist:[],
isTemp:true,
next:function(){
2011-08-12 10:34:02 +00:00
var items=(PlayList.isTemp)?PlayList.tempPlaylist:PlayList.items;
var next=PlayList.current+1;
2011-08-12 10:34:02 +00:00
if(next>=items.length){
next=0;
}
PlayList.play(next);
PlayList.render();
},
previous:function(){
2011-08-12 10:34:02 +00:00
var items=(PlayList.isTemp)?PlayList.tempPlaylist:PlayList.items;
var next=PlayList.current-1;
if(next<0){
2011-08-12 10:34:02 +00:00
next=items.length-1;
}
PlayList.play(next);
PlayList.render();
},
play:function(index,time,ready){
2011-08-12 10:34:02 +00:00
var items=(PlayList.isTemp)?PlayList.tempPlaylist:PlayList.items;
if(index==null){
index=PlayList.current;
}
2011-08-12 10:34:02 +00:00
if(index>-1 && index<items.length){
PlayList.current=index;
if(PlayList.player){
2011-08-12 10:34:02 +00:00
if(PlayList.player.data('jPlayer').options.supplied!=items[index].type){//the the audio type changes we need to reinitialize jplayer
PlayList.player.jPlayer("destroy");
2011-08-12 10:34:02 +00:00
PlayList.init(items[index].type,function(){PlayList.play(null,time,ready)});
}else{
2011-08-12 10:34:02 +00:00
PlayList.player.jPlayer("setMedia", items[PlayList.current]);
items[index].playcount++;
PlayList.player.jPlayer("play",time);
2011-07-31 23:56:45 +00:00
if(index>0){
var previous=index-1;
}else{
2011-08-12 10:34:02 +00:00
var previous=items.length-1;
}
2011-08-12 10:34:02 +00:00
if(index+1<items.length){
var next=index+1;
}else{
var next=0;
}
2011-08-12 10:34:02 +00:00
$('.jp-next').attr('title',items[next].name);
$('.jp-previous').attr('title',items[previous].name);
if (typeof Collection !== 'undefined') {
Collection.registerPlay();
}
if(ready){
ready();
}
}
}else{
2011-08-12 10:34:02 +00:00
PlayList.init(items[index].type,PlayList.play);
}
}
},
init:function(type,ready){
if(!PlayList.player){
$(".jp-previous").click(function() {
PlayList.previous();
$(this).blur();
PlayList.render();
return false;
});
$(".jp-next").click(function() {
PlayList.next();
$(this).blur();
PlayList.render();
return false;
});
2011-08-11 09:27:14 +00:00
PlayList.player=$('#controls div.player');
}
$(PlayList.player).jPlayer({
ended:PlayList.next,
pause:function(){
localStorage.setItem(oc_current_user+'oc_playlist_playing','false');
},
play:function(){
localStorage.setItem(oc_current_user+'oc_playlist_playing','true');
},
supplied:type,
ready:function(){
PlayList.load();
if(ready){
ready();
}
},
volume:PlayList.volume,
2011-08-11 09:27:14 +00:00
cssSelectorAncestor:'#controls',
swfPath:OC.linkTo('media','js'),
});
},
2011-08-12 10:34:02 +00:00
add:function(song,temp,dontReset){
if(!dontReset){
PlayList.tempPlaylist=[];//clear the temp playlist
}
PlayList.isTemp=temp;
PlayList.isTemp=true;
if(!song){
return;
}
if(song.substr){//we are passed a string, asume it's a url to a song
2011-08-12 10:34:02 +00:00
PlayList.addFile(song,temp,true);
}
if(song.albums){//a artist object was passed, add all albums inside it
$.each(song.albums,function(index,album){
2011-08-12 10:34:02 +00:00
PlayList.add(album,temp,true);
});
}
if(song.songs){//a album object was passed, add all songs inside it
$.each(song.songs,function(index,song){
2011-08-12 10:34:02 +00:00
PlayList.add(song,temp,true);
});
}
if(song.song_name){
var type=musicTypeFromFile(song.song_path);
var item={name:song.song_name,type:type,artist:song.artist_name,album:song.album_name,length:song.song_length,playcount:song.song_playcount};
item[type]=PlayList.urlBase+encodeURIComponent(song.song_path);
2011-08-12 10:34:02 +00:00
if(PlayList.isTemp){
PlayList.tempPlaylist.push(item);
}else{
PlayList.items.push(item);
}
}
},
addFile:function(path){
var type=musicTypeFromFile(path);
var item={name:'unknown',artist:'unknown',album:'unknwon',type:type};
$.getJSON(OC.filePath('media','ajax','api.php')+'?action=get_path_info&path='+encodeURIComponent(path),function(song){
item.name=song.song_name;
item.artist=song.artist;
item.album=song.album;
});
item[type]=PlayList.urlBase+encodeURIComponent(path);
PlayList.items.push(item);
},
remove:function(index){
PlayList.items.splice(index,1);
PlayList.render();
},
render:function(){},
playing:function(){
if(!PlayList.player){
return false;
}else{
return !PlayList.player.data("jPlayer").status.paused;
}
},
save:function(){
if(typeof localStorage !== 'undefined' && localStorage){
2011-07-31 23:56:45 +00:00
localStorage.setItem(oc_current_user+'oc_playlist_items',JSON.stringify(PlayList.items));
localStorage.setItem(oc_current_user+'oc_playlist_current',PlayList.current);
var time=Math.round(PlayList.player.data('jPlayer').status.currentTime);
localStorage.setItem(oc_current_user+'oc_playlist_time',time);
var volume=PlayList.player.data('jPlayer').options.volume*100;
localStorage.setItem(oc_current_user+'oc_playlist_volume',volume);
if(PlayList.active){
localStorage.setItem(oc_current_user+'oc_playlist_active','false');
}
}
},
load:function(){
if(typeof localStorage !== 'undefined' && localStorage){
PlayList.active=true;
localStorage.setItem(oc_current_user+'oc_playlist_active','true');
2011-07-31 23:56:45 +00:00
if(localStorage.hasOwnProperty(oc_current_user+'oc_playlist_items')){
PlayList.items=JSON.parse(localStorage.getItem(oc_current_user+'oc_playlist_items'));
if(PlayList.items.length>0){
PlayList.current=parseInt(localStorage.getItem(oc_current_user+'oc_playlist_current'));
var time=parseInt(localStorage.getItem(oc_current_user+'oc_playlist_time'));
if(localStorage.hasOwnProperty(oc_current_user+'oc_playlist_volume')){
var volume=localStorage.getItem(oc_current_user+'oc_playlist_volume');
PlayList.volume=volume/100;
$('.jp-volume-bar-value').css('width',volume+'%');
if(PlayList.player.data('jPlayer')){
PlayList.player.jPlayer("option",'volume',volume/100);
}
}
if(JSON.parse(localStorage.getItem(oc_current_user+'oc_playlist_playing'))){
PlayList.play(null,time);
}else{
PlayList.play(null,time,function(){
PlayList.player.jPlayer("pause");
});
}
PlayList.render();
}
}
}
}
}
$(document).ready(function(){
$(window).bind('beforeunload', function (){
PlayList.save();
});
})