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

	$('jp-previous').tipsy({gravity:'n', fade:true, live:true});
	$('jp-next').tipsy({gravity:'n', fade:true, live:true});
})