![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Jplayer代理类
1![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//**
2
*
3
* Media 代理
4
* @param Object p_play:播放器
5
*/
6![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
var media_player_proxy = function(p_play)
{
7
var _play = p_play;
8
var _progress_change;
9
var _sound_complete;
10
11![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
var progress_change = function()
{
12![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(_progress_change)
{
13
//进度改变事件
14
//lp:已下载百分数; ppr:; ppa:已播放百分数 pt:播放时间; tt:总时间
15
var lp = _play.network.downloadProgress;
16
var ppr;
17![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
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![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(Number(_play.currentMedia.duration - _play.controls.currentPosition) < 0.3)
{
24
25![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(_sound_complete)
{
26
_sound_complete();
27
}
28
}
29
}
30
}
31
}
32
//改变路径(私)
33![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
var change = function(p_url)
{
34
_play.url = p_url;
35
_play.controls.play();
36
}
37
38
//播放
39![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
this.play = function()
{
40
_play.controls.play();
41
},
42
//改变并播放
43![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
this.changeAndPlay = function(p_url)
{
44
change(p_url);
45
_play.controls.play();
46
}
47
//暂停
48![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
this.pause = function()
{
49
_play.controls.pause();
50
},
51
//停止
52![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
this.stop = function()
{
53
_play.controls.stop();
54
},
55
//设置音量
56![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
this.volume = function(p_value)
{
57
_play.settings.volume = p_value
58
},
59
//设置进度
60![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
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![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
this.onProgressChange = function(p_handler)
{
67
_progress_change = p_handler;
68
},
69
//播放完成事件
70![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
this.onSoundComplete = function(p_handler)
{
71
_sound_complete = p_handler;
72
},
73
//播放加载事件
74![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
this.mPlayer = function(p_obj)
{
75![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(p_obj)
{
76
p_obj.ready();
77
}
78
window.setInterval(progress_change,100);
79
}
80
}
81![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
82![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
83![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
var 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![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
var play_index = function(index)
{
98![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(_datalist.length > 0)
{
99
var url = _datalist[index]["filepath"];
100
_player.changeAndPlay(url);
101
_play_index = index;
102![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(_play_handler)
{
103
_play_handler(_datalist[index]);
104
}
105
}
106
}
107
108
//播放第一首
109![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
var play_first = function()
{
110![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(_datalist.length > 0)
{
111
play_index(0);
112
}
113
}
114
115
//播放下一首
116![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
var play_next = function()
{
117
var n = get_play_index_num();
118![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(n < _datalist.length)
{
119
play_index(n);
120
}
121![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
else
{
122
play_index(0);
123
}
124
}
125
126
//播放上一首
127![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
var play_previous = function()
{
128
var n = get_play_index_num(true);
129![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(n >= 0)
{
130
play_index(n);
131
}
132
}
133
134![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
var show_play_btn = function()
{
135
$("#" + _btn_obj["play"]).hide();
136
$("#" + _btn_obj["pause"]).show();
137
$("#" + _btn_obj["stop"]).show();
138
}
139
140![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
var show_pause_btn = function()
{
141
$("#" + _btn_obj["play"]).show();
142
$("#" + _btn_obj["pause"]).hide();
143
$("#" + _btn_obj["stop"]).show();
144
}
145
146![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
var show_stop_btn = function()
{
147
$("#" + _btn_obj["play"]).show();
148
$("#" + _btn_obj["pause"]).hide();
149
}
150
151
//验证Key是否存在
152![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
var check_key = function(key)
{
153![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
for(var i = 0; i < _datalist.length;i++)
{
154
var item = _datalist[i];
155![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(item.key == key)
{
156
return true;
157
}
158
}
159
return false;
160
}
161
162![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
var get_play_index_num = function(is_pre)
{
163
var i = _play_index;
164![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(is_pre)
{
165![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
switch(_play_pattern)
{
166
case 0:
167
break;
168
case 1:
169
i-=1;
170
break;
171
case 2:
172
i-=1;
173![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
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![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
else
{
184![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
switch(_play_pattern)
{
185
case 0:
186
break;
187
case 1:
188
i+=1;
189
break;
190
case 2:
191
i+=1;
192![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
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![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
this.control_sound = function(p_has)
{
207![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(p_has == undefined)
{
208
return _is_no_sound;
209
}
210
_is_no_sound = p_has;
211![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(_is_no_sound)
{
212
_player.volume(0);
213![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
}else
{
214
_player.volume(_volume);
215
}
216
},
217
//播放进度事件
218![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
this.on_progress_change = function(fun)
{
219![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(_progress_change)
{
220![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
_progress_change = function()
{
221
_progress_change();
222
fun();
223
}
224
}
225![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
else
{
226
_progress_change = fun;
227
}
228
},
229
//获得正在播放歌曲对象
230![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
this.get_active_music = function()
{
231![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(_play_index != -1 && _datalist.length > 0)
{
232
return _datalist[_play_index];
233
}
234
return null;
235
},
236
//播放
237![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
this.play = function(p_key)
{
238![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
for(var i = 0; i < _datalist.length; i++)
{
239![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(p_key == _datalist[i].key)
{
240
play_index(i);
241
}
242
}
243
},
244![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
this.play_first_music = function()
{
245
play_first();
246
},
247
//增加歌曲 必须字段 key: 音乐标识; filepath: 路径; name: 音乐名
248![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
this.add_data = function(p_key,p_name,p_filepath)
{
249![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
var obj =
{key:p_key,filepath:p_filepath,name:p_name};
250
var result_obj = [];
251
var is_add = false;
252![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(!check_key(obj.key))
{
253
_datalist.push(obj);
254
result_obj.push(obj);
255
is_add = true;
256
}
257![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(_data_change_handler)
{
258
_data_change_handler(is_add,result_obj);
259
}
260
},
261
//增加歌曲(批量)
262![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
this.add_dataarr = function(p_obj_arr)
{
263
var is_add = false;
264
var result_obj = [];
265![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
for(var i = 0; i < p_obj_arr.length; i++)
{
266![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
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![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(_data_change_handler)
{
273
_data_change_handler(is_add,result_obj);
274
}
275
return is_add;
276
},
277
//删除歌曲
278![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
this.delete_data = function(p_key)
{
279
var index = -1;
280![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
for(var i = 0; i < _datalist.length;i++)
{
281
var item = _datalist[i];
282![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(item.key == p_key)
{
283
index = i;
284
}
285
}
286
var key = -1;
287![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(index != -1)
{
288
key = _datalist[index].key;
289![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(_data_change_handler)
{
290![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
_data_change_handler(true,[
{key:key}],true);
291
}
292
_datalist.splice(index,1);
293
}
294![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
else
{
295![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(_data_change_handler)
{
296![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
_data_change_handler(false,
{key:key},true);
297
}
298
}
299
},
300
//绑定按钮
301![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
this.bind_btn = function(obj)
{
302![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(obj)
{
303
_btn_obj = obj;
304![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
$.each(_btn_obj, function(i, n)
{
305
var ele = $("#" + n);
306![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(ele == undefined)
{
307
ele = $(n);
308
}
309![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
ele.click(function()
{
310![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
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![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
this.set_volume = function(p_num)
{
338![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(p_num == undefined)
{
339
return _volume;
340
}
341
_volume = p_num;
342
_player.volume(_is_no_sound? 0 : _volume);
343
},
344
//设置进度
345![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
this.set_plan = function(p_num)
{
346
_player.playHead(p_num);
347
},
348
//播放事件
349![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
this.set_play_call_back = function(p_fun)
{
350
_play_handler = p_fun;
351
},
352
//设置列表改变事件
353![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
this.set_data_change_handler = function(p_fun)
{
354![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(p_fun)
{
355
_data_change_handler = p_fun;
356
}
357
},
358
//设置播放模式
359![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
this.pattern = function(p_pattern)
{
360![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(p_pattern == undefined)
{
361
return _play_pattern;
362
}
363
_play_pattern = p_pattern;
364
},
365
//获取列表
366![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
this.get_datasource = function()
{
367
return _datalist;
368
},
369![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
this.get_player = function()
{
370
return _player;
371
},
372
//初始化
373![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
this.init = function(p_playId,p_play_callback)
{
374![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if($.browser.msie)
{
375
_player = new media_player_proxy(document.getElementById(p_playId));
376
}
377![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
else
{
378
_player = $("#" + p_playId);
379
}
380![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(p_play_callback)
{
381
_play_handler = p_play_callback;
382
}
383![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(_progress_change)
{
384
_player.onProgressChange(_progress_change);
385
}
386![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
_player.onSoundComplete(function()
{
387
var i = get_play_index_num();
388
play_index(i);
389
});
390![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if($.browser.msie)
{
391![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
_player.mPlayer(
{
392![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
ready: function ()
{
393![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(_play_index == -1)
{
394
play_first();
395
show_play_btn();
396
}
397![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
else
{
398
show_stop_btn();
399
}
400
}
401
});
402
}
403![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
else
{
404![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
_player.jPlayer(
{
405![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
ready: function ()
{
406![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(_play_index == -1)
{
407
play_first();
408
show_play_btn();
409
}
410![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
else
{
411
show_stop_btn();
412
}
413
},
414
swfPath: URL_STATIC+"/media/music"
415
});
416
}
417
}
418
}
使用方法:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
var player = new player_proxy();
player.bind_btn({//绑定BOTTON
play:"play_btn", //播放
pause:"pause_btn",//暂停
stop:"stop_btn",//停止
pre:"pre_btn",//上一首
next:"next_btn"//下一首
});
//音乐数据格式 [{key:音乐标识, filepath:路径, name:音乐名}]
player.set_data_change_handler(function(issuccess,arr,isdel){ //改变音乐数据后,执行的函数
//issuccess: 是否成功
//arr: 音乐数据改变的列表
//isdel: true为删除数据
});
player.on_progress_change(function(lp,ppr,ppa,pt,tt){ //进步改变后,执行的函数
//lp:已下载百分数; ppr:; ppa:已播放百分数 pt:播放时间; tt:总时间
});
player.init(playId,play_callback); //playId:放置播放器的层ID,IE下的HTML需要加入media player object 如以下HTML; play_callback: 播放歌曲时,执行的函数
/*
<object id="media_player" width="350" height="64" classid="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6"
codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,4,7,1112"
align="baseline" border="0" standby="Loading Microsoft Windows Media Player components
"
type="application/x-oleobject">
<param name="URL" value="">
<param name="autoStart" value="true">
<param name="invokeURLs" value="false">
<param name="playCount" value="100">
<param name="defaultFrame" value="datawindow">
</object>
*/
//除此之外,还提供了以下方法
player.control_sound(p_has); //p_has: true设置静音,false开启声音
player.add_dataarr(obj_arr);//增加歌曲(批量) 音乐数据:数组
一个人能够走多远,取决于与谁同行
posted @
2009-07-29 10:54
Max Gan
阅读(
789)
评论()
编辑
收藏
举报