Jplayer代理类
1/**//**
2*
3* Media 代理
4* @param Object p_play:播放器
5*/
6var media_player_proxy = function(p_play){
7 var _play = p_play;
8 var _progress_change;
9 var _sound_complete;
10
11 var progress_change = function(){
12 if(_progress_change){
13 //进度改变事件
14 //lp:已下载百分数; ppr:; ppa:已播放百分数 pt:播放时间; tt:总时间
15 var lp = _play.network.downloadProgress;
16 var ppr;
17 if(_play.playState == 3){
18 var ppa = Number(_play.controls.currentPosition) / Number(_play.currentMedia.duration) * 100;
19 var pt = Number(_play.controls.currentPosition) * 1000;
20 var tt = Number(_play.currentMedia.duration) * 1000;
21 _progress_change(lp,ppr,ppa,pt,tt);
22
23 if(Number(_play.currentMedia.duration - _play.controls.currentPosition) < 0.3){
24
25 if(_sound_complete){
26 _sound_complete();
27 }
28 }
29 }
30 }
31 }
32 //改变路径(私)
33 var change = function(p_url){
34 _play.url = p_url;
35 _play.controls.play();
36 }
37
38 //播放
39 this.play = function(){
40 _play.controls.play();
41 },
42 //改变并播放
43 this.changeAndPlay = function(p_url){
44 change(p_url);
45 _play.controls.play();
46 }
47 //暂停
48 this.pause = function(){
49 _play.controls.pause();
50 },
51 //停止
52 this.stop = function(){
53 _play.controls.stop();
54 },
55 //设置音量
56 this.volume = function(p_value){
57 _play.settings.volume = p_value
58 },
59 //设置进度
60 this.playHead = function(p_num){
61 //p_num 百分比
62 _play.controls.currentPosition = Math.ceil(p_num / 100 * _play.currentMedia.duration);
63 progress_change();
64 },
65 //进度改变事件
66 this.onProgressChange = function(p_handler){
67 _progress_change = p_handler;
68 },
69 //播放完成事件
70 this.onSoundComplete = function(p_handler){
71 _sound_complete = p_handler;
72 },
73 //播放加载事件
74 this.mPlayer = function(p_obj){
75 if(p_obj){
76 p_obj.ready();
77 }
78 window.setInterval(progress_change,100);
79 }
80}
81
82
83var player_proxy = function(){
84 var _datalist = []; //音乐列表
85 var _btn_obj; //按钮对象
86 var _show_state = -1; //-1:没有音乐 0:正在播入 1:暂停中 2:已停止
87 var _progress_change;
88 var _player;
89 var _play_index = -1; //播放序号
90 var _volume = 50; //默认音量
91 var _play_handler;
92 var _is_no_sound = false;
93 var _play_pattern = 2; //播放模式 0:单曲循环; 1:顺序; 2:循环; 3:随机
94 var _data_change_handler; //单乐数据改变事件
95
96 //根据序号播放歌曲
97 var play_index = function(index){
98 if(_datalist.length > 0){
99 var url = _datalist[index]["filepath"];
100 _player.changeAndPlay(url);
101 _play_index = index;
102 if(_play_handler){
103 _play_handler(_datalist[index]);
104 }
105 }
106 }
107
108 //播放第一首
109 var play_first = function(){
110 if(_datalist.length > 0){
111 play_index(0);
112 }
113 }
114
115 //播放下一首
116 var play_next = function(){
117 var n = get_play_index_num();
118 if(n < _datalist.length){
119 play_index(n);
120 }
121 else{
122 play_index(0);
123 }
124 }
125
126 //播放上一首
127 var play_previous = function(){
128 var n = get_play_index_num(true);
129 if(n >= 0){
130 play_index(n);
131 }
132 }
133
134 var show_play_btn = function(){
135 $("#" + _btn_obj["play"]).hide();
136 $("#" + _btn_obj["pause"]).show();
137 $("#" + _btn_obj["stop"]).show();
138 }
139
140 var show_pause_btn = function(){
141 $("#" + _btn_obj["play"]).show();
142 $("#" + _btn_obj["pause"]).hide();
143 $("#" + _btn_obj["stop"]).show();
144 }
145
146 var show_stop_btn = function(){
147 $("#" + _btn_obj["play"]).show();
148 $("#" + _btn_obj["pause"]).hide();
149 }
150
151 //验证Key是否存在
152 var check_key = function(key){
153 for(var i = 0; i < _datalist.length;i++){
154 var item = _datalist[i];
155 if(item.key == key){
156 return true;
157 }
158 }
159 return false;
160 }
161
162 var get_play_index_num = function(is_pre){
163 var i = _play_index;
164 if(is_pre){
165 switch(_play_pattern){
166 case 0:
167 break;
168 case 1:
169 i-=1;
170 break;
171 case 2:
172 i-=1;
173 if(i < 0){
174 i = 0;
175 }
176 play_index(i);
177 break;
178 case 3:
179 i = parseInt(Math.random()*(_datalist.length-1));
180 break;
181 }
182 }
183 else{
184 switch(_play_pattern){
185 case 0:
186 break;
187 case 1:
188 i+=1;
189 break;
190 case 2:
191 i+=1;
192 if(i >= _datalist.length){
193 i = 0;
194 }
195 play_index(i);
196 break;
197 case 3:
198 i = parseInt(Math.random()*(_datalist.length-1));
199 break;
200 }
201 }
202 return i;
203 }
204
205 //控制静音状态
206 this.control_sound = function(p_has){
207 if(p_has == undefined){
208 return _is_no_sound;
209 }
210 _is_no_sound = p_has;
211 if(_is_no_sound){
212 _player.volume(0);
213 }else{
214 _player.volume(_volume);
215 }
216 },
217 //播放进度事件
218 this.on_progress_change = function(fun){
219 if(_progress_change){
220 _progress_change = function(){
221 _progress_change();
222 fun();
223 }
224 }
225 else{
226 _progress_change = fun;
227 }
228 },
229 //获得正在播放歌曲对象
230 this.get_active_music = function(){
231 if(_play_index != -1 && _datalist.length > 0){
232 return _datalist[_play_index];
233 }
234 return null;
235 },
236 //播放
237 this.play = function(p_key){
238 for(var i = 0; i < _datalist.length; i++){
239 if(p_key == _datalist[i].key){
240 play_index(i);
241 }
242 }
243 },
244 this.play_first_music = function(){
245 play_first();
246 },
247 //增加歌曲 必须字段 key: 音乐标识; filepath: 路径; name: 音乐名
248 this.add_data = function(p_key,p_name,p_filepath){
249 var obj = {key:p_key,filepath:p_filepath,name:p_name};
250 var result_obj = [];
251 var is_add = false;
252 if(!check_key(obj.key)){
253 _datalist.push(obj);
254 result_obj.push(obj);
255 is_add = true;
256 }
257 if(_data_change_handler){
258 _data_change_handler(is_add,result_obj);
259 }
260 },
261 //增加歌曲(批量)
262 this.add_dataarr = function(p_obj_arr){
263 var is_add = false;
264 var result_obj = [];
265 for(var i = 0; i < p_obj_arr.length; i++){
266 if(!check_key(p_obj_arr[i].key)){
267 _datalist.push(p_obj_arr[i]);
268 result_obj.push(p_obj_arr[i]);
269 is_add = true;
270 }
271 }
272 if(_data_change_handler){
273 _data_change_handler(is_add,result_obj);
274 }
275 return is_add;
276 },
277 //删除歌曲
278 this.delete_data = function(p_key){
279 var index = -1;
280 for(var i = 0; i < _datalist.length;i++){
281 var item = _datalist[i];
282 if(item.key == p_key){
283 index = i;
284 }
285 }
286 var key = -1;
287 if(index != -1){
288 key = _datalist[index].key;
289 if(_data_change_handler){
290 _data_change_handler(true,[{key:key}],true);
291 }
292 _datalist.splice(index,1);
293 }
294 else{
295 if(_data_change_handler){
296 _data_change_handler(false,{key:key},true);
297 }
298 }
299 },
300 //绑定按钮
301 this.bind_btn = function(obj){
302 if(obj){
303 _btn_obj = obj;
304 $.each(_btn_obj, function(i, n){
305 var ele = $("#" + n);
306 if(ele == undefined){
307 ele = $(n);
308 }
309 ele.click(function(){
310 switch(i){
311 case "play": //播放
312 _player.play();
313 show_play_btn();
314 break;
315 case "pause": //暂停
316 _player.pause();
317 show_pause_btn();
318 break;
319 case "stop": //停止
320 _player.stop();
321 show_stop_btn()
322 break;
323 case "pre": //前一首
324 play_previous();
325 show_play_btn();
326 break;
327 case "next": //下一首
328 play_next();
329 show_play_btn();
330 break;
331 }
332 });
333 });
334 }
335 },
336 //设置音量
337 this.set_volume = function(p_num){
338 if(p_num == undefined){
339 return _volume;
340 }
341 _volume = p_num;
342 _player.volume(_is_no_sound? 0 : _volume);
343 },
344 //设置进度
345 this.set_plan = function(p_num){
346 _player.playHead(p_num);
347 },
348 //播放事件
349 this.set_play_call_back = function(p_fun){
350 _play_handler = p_fun;
351 },
352 //设置列表改变事件
353 this.set_data_change_handler = function(p_fun){
354 if(p_fun){
355 _data_change_handler = p_fun;
356 }
357 },
358 //设置播放模式
359 this.pattern = function(p_pattern){
360 if(p_pattern == undefined){
361 return _play_pattern;
362 }
363 _play_pattern = p_pattern;
364 },
365 //获取列表
366 this.get_datasource = function(){
367 return _datalist;
368 },
369 this.get_player = function(){
370 return _player;
371 },
372 //初始化
373 this.init = function(p_playId,p_play_callback){
374 if($.browser.msie){
375 _player = new media_player_proxy(document.getElementById(p_playId));
376 }
377 else{
378 _player = $("#" + p_playId);
379 }
380 if(p_play_callback){
381 _play_handler = p_play_callback;
382 }
383 if(_progress_change){
384 _player.onProgressChange(_progress_change);
385 }
386 _player.onSoundComplete(function(){
387 var i = get_play_index_num();
388 play_index(i);
389 });
390 if($.browser.msie){
391 _player.mPlayer({
392 ready: function () {
393 if(_play_index == -1){
394 play_first();
395 show_play_btn();
396 }
397 else{
398 show_stop_btn();
399 }
400 }
401 });
402 }
403 else{
404 _player.jPlayer({
405 ready: function () {
406 if(_play_index == -1){
407 play_first();
408 show_play_btn();
409 }
410 else{
411 show_stop_btn();
412 }
413 },
414 swfPath: URL_STATIC+"/media/music"
415 });
416 }
417 }
418}
转载自原文链接, 如需删除请联系管理员。
原文链接:JQuery播放器代理--IE下支持wma格式,转载请注明来源!
相关推荐