player.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. var PlayList={
  2. urlBase:OC.linkTo('media','ajax/api.php')+'?action=play&path=',
  3. current:-1,
  4. items:[],
  5. player:null,
  6. volume:0.8,
  7. active:false,
  8. next:function(){
  9. var items=PlayList.items;
  10. var next=PlayList.current+1;
  11. if(next>=items.length){
  12. next=0;
  13. }
  14. PlayList.play(next);
  15. PlayList.render();
  16. },
  17. previous:function(){
  18. var items=PlayList.items;
  19. var next=PlayList.current-1;
  20. if(next<0){
  21. next=items.length-1;
  22. }
  23. PlayList.play(next);
  24. PlayList.render();
  25. },
  26. play:function(index,time,ready){
  27. var items=PlayList.items;
  28. if(index==null){
  29. index=PlayList.current;
  30. }
  31. PlayList.save();
  32. if(index>-1 && index<items.length){
  33. PlayList.current=index;
  34. if(PlayList.player){
  35. if(PlayList.player.data('jPlayer').options.supplied!=items[index].type){//the the audio type changes we need to reinitialize jplayer
  36. PlayList.player.jPlayer("play",time);
  37. OC.localStorage.setItem('playlist_time',time);
  38. PlayList.player.jPlayer("destroy");
  39. // PlayList.save(); // so that the init don't lose the playlist
  40. PlayList.init(items[index].type,null); // init calls load that calls play
  41. }else{
  42. PlayList.player.jPlayer("setMedia", items[PlayList.current]);
  43. $(".jp-current-song").text(items[PlayList.current].name);
  44. items[index].playcount++;
  45. PlayList.player.jPlayer("play",time);
  46. if(index>0){
  47. var previous=index-1;
  48. }else{
  49. var previous=items.length-1;
  50. }
  51. if(index+1<items.length){
  52. var next=index+1;
  53. }else{
  54. var next=0;
  55. }
  56. $('.jp-next').attr('title',items[next].name);
  57. $('.jp-previous').attr('title',items[previous].name);
  58. if (typeof Collection !== 'undefined') {
  59. Collection.registerPlay();
  60. }
  61. PlayList.render();
  62. if(ready){
  63. ready();
  64. }
  65. }
  66. }else{
  67. OC.localStorage.setItem('playlist_time',time);
  68. OC.localStorage.setItem('playlist_playing',true);
  69. PlayList.init(items[index].type,null); // init calls load that calls play
  70. }
  71. }
  72. $(".song").removeClass("collection_playing");
  73. $(".jp-playlist-" + index).addClass("collection_playing");
  74. },
  75. init:function(type,ready){
  76. if(!PlayList.player){
  77. $(".jp-previous").click(function() {
  78. PlayList.previous();
  79. $(this).blur();
  80. PlayList.render();
  81. return false;
  82. });
  83. $(".jp-next").click(function() {
  84. PlayList.next();
  85. $(this).blur();
  86. PlayList.render();
  87. return false;
  88. });
  89. PlayList.player=$('#jp-player');
  90. }
  91. $(PlayList.player).jPlayer({
  92. ended:PlayList.next,
  93. pause:function(){
  94. OC.localStorage.setItem('playlist_playing',false);
  95. document.title = "ownCloud";
  96. },
  97. play:function(event){
  98. OC.localStorage.setItem('playlist_playing',true);
  99. document.title = "\u25b8 " + event.jPlayer.status.media.name + " - " + event.jPlayer.status.media.artist + " - ownCloud";
  100. },
  101. supplied:type,
  102. ready:function(){
  103. PlayList.load();
  104. if(ready){
  105. ready();
  106. }
  107. },
  108. volume:PlayList.volume,
  109. cssSelectorAncestor:'.player-controls',
  110. swfPath:OC.linkTo('media','js'),
  111. });
  112. },
  113. add:function(song,dontReset){
  114. if(!dontReset){
  115. PlayList.items=[];//clear the playlist
  116. }
  117. if(!song){
  118. return;
  119. }
  120. if(song.substr){//we are passed a string, asume it's a url to a song
  121. PlayList.addFile(song,true);
  122. }
  123. if(song.albums){//a artist object was passed, add all albums inside it
  124. $.each(song.albums,function(index,album){
  125. PlayList.add(album,true);
  126. });
  127. } else if(song.songs){//a album object was passed, add all songs inside it
  128. $.each(song.songs,function(index,song){
  129. PlayList.add(song,true);
  130. });
  131. }
  132. if(song.path){
  133. var type=musicTypeFromFile(song.path);
  134. var item={name:song.name,type:type,artist:song.artist,album:song.album,length:song.length,playcount:song.playCount};
  135. item[type]=PlayList.urlBase+encodeURIComponent(song.path);
  136. PlayList.items.push(item);
  137. }
  138. },
  139. addFile:function(path){
  140. var type=musicTypeFromFile(path);
  141. var item={name:'unknown',artist:'unknown',album:'unknwon',type:type};
  142. $.getJSON(OC.filePath('media','ajax','api.php')+'?action=get_path_info&path='+encodeURIComponent(path),function(song){
  143. item.name=song.song_name;
  144. item.artist=song.artist;
  145. item.album=song.album;
  146. });
  147. item[type]=PlayList.urlBase+encodeURIComponent(path);
  148. PlayList.items.push(item);
  149. },
  150. remove:function(index){
  151. PlayList.items.splice(index,1);
  152. PlayList.render();
  153. },
  154. render:function(){},
  155. playing:function(){
  156. if(!PlayList.player){
  157. return false;
  158. }else{
  159. return !PlayList.player.data("jPlayer").status.paused;
  160. }
  161. },
  162. save:function(){
  163. OC.localStorage.setItem('playlist_items',PlayList.items);
  164. OC.localStorage.setItem('playlist_current',PlayList.current);
  165. if(PlayList.player) {
  166. if(PlayList.player.data('jPlayer')) {
  167. var time=Math.round(PlayList.player.data('jPlayer').status.currentTime);
  168. OC.localStorage.setItem('playlist_time',time);
  169. var volume=PlayList.player.data('jPlayer').options.volume*100;
  170. OC.localStorage.setItem('playlist_volume',volume);
  171. }
  172. }
  173. OC.localStorage.setItem('playlist_active',true);
  174. },
  175. load:function(){
  176. PlayList.active=true;
  177. OC.localStorage.setItem('playlist_active',true);
  178. if(OC.localStorage.hasItem('playlist_items')){
  179. PlayList.items=OC.localStorage.getItem('playlist_items');
  180. if(PlayList.items && PlayList.items.length>0){
  181. PlayList.current=OC.localStorage.getItem('playlist_current');
  182. var time=OC.localStorage.getItem('playlist_time');
  183. if(OC.localStorage.hasItem('playlist_volume')){
  184. var volume=OC.localStorage.getItem('playlist_volume');
  185. PlayList.volume=volume/100;
  186. $('.jp-volume-bar-value').css('width',volume+'%');
  187. if(PlayList.player.data('jPlayer')){
  188. PlayList.player.jPlayer("option",'volume',volume/100);
  189. }
  190. }
  191. if(OC.localStorage.getItem('playlist_playing')){
  192. PlayList.play(null,time);
  193. }else{
  194. PlayList.play(null,time,function(){
  195. PlayList.player.jPlayer("pause");
  196. });
  197. }
  198. PlayList.render();
  199. }
  200. }
  201. }
  202. }
  203. $(document).ready(function(){
  204. $(window).bind('beforeunload', function (){
  205. PlayList.save();
  206. if(PlayList.active){
  207. OC.localStorage.setItem('playlist_active',false);
  208. }
  209. });
  210. $('jp-previous').tipsy({gravity:'n', fade:true, live:true});
  211. $('jp-next').tipsy({gravity:'n', fade:true, live:true});
  212. })