summaryrefslogtreecommitdiffstats
path: root/apps/media/js/player.js
blob: f76628110a841f4515e694959ab8e8dbcee8c5ed (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
var PlayList={
	urlBase:OC.linkTo('media','ajax/api.php')+'?action=play&path=',
	current:-1,
	items:[],
	player:null,
	parent:null,
	next:function(){
		var next=PlayList.current+1;
		if(next>=PlayList.items.length){
			next=0;
		}
		PlayList.play(next);
	},
	previous:function(){
		var next=PlayList.current-1;
		if(next<0){
			next=PlayList.items.length-1;
		}
		PlayList.play(next);
	},
	play:function(index){
		if(index==null){
			index=PlayList.current;
		}
		if(index>-1 && index<PlayList.items.length){
			PlayList.current=index;
			if(PlayList.player){
				if(PlayList.player.data('jPlayer').options.supplied!=PlayList.items[index].type){//the the audio type changes we need to reinitialize jplayer
					PlayList.player.jPlayer("destroy");
					PlayList.init(PlayList.items[index].type,PlayList.play);
				}else{
					PlayList.player.jPlayer("setMedia", PlayList.items[PlayList.current]);
					PlayList.player.jPlayer("play");
				}
			}else{
				PlayList.init(PlayList.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;
			});
			PlayList.player=$('#jp-interface div.player');
		}
		$(PlayList.player).jPlayer({
			ended:PlayList.next,
			supplied:type,
			ready:function(){
				if(ready){
					ready();
				}
			},
			cssSelectorAncestor:'#jp-interface',
			swfPath:OC.linkTo('media','js'),
		});
	},
	add:function(song){
		if(song.substr){//we are passed a string, asume it's a url to a song
			PlayList.addFile(song);
		}
		if(song.albums){//a artist object was passed, add all albums inside it
			$.each(song.albums,function(index,album){
				PlayList.add(album);
			});
		}
		if(song.songs){//a album object was passed, add all songs inside it
			$.each(song.songs,function(index,song){
				PlayList.add(song);
			});
		}
		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};
			item[type]=PlayList.urlBase+encodeURIComponent(song.song_path);
			PlayList.items.push(item);
		}
	},
	addFile:function(path){
		var type=musicTypeFromFile(path);
		var item={name:'unknown',artist:'unknown',album:'unknwon',type:type};//todo get song data
		item[type]=PlayList.urlBase+encodeURIComponent(path);
		PlayList.items.push(item);
	},
	render:function(parent){//parent should be an ul element
		if(parent){
			PlayList.parent=parent;
		}else{
			parent=PlayList.parent;
		}
		if(parent){
			parent.empty();
			for(var i=0;i<PlayList.items.length;i++){
				var song=PlayList.items[i];
				var item=$('<li>'+song.artist+' - '+song.album+' - '+song.name+'</li>');
				item.data('artist',song.artist);
				item.data('album',song.album);
				item.data('name',song.name);
				item.data('index',i);
				item.click(function(){
					PlayList.play($(this).data('index'));
					PlayList.render();
				});
				if(i==PlayList.current){
					item.addClass('current');
				}
				parent.append(item);
			}
		}
	}
}