首页 » 技术分享 » JQuery播放器代理--IE下支持wma格式

JQuery播放器代理--IE下支持wma格式

 
 
ContractedBlock.gif
ExpandedBlockStart.gif
Jplayer代理类

  1ExpandedBlockStart.gifContractedBlock.gif/**//**
  2*
  3* Media 代理
  4* @param Object p_play:播放器
  5*/

  6ExpandedBlockStart.gifContractedBlock.gifvar media_player_proxy = function(p_play){
  7    var _play = p_play;
  8    var _progress_change;
  9    var _sound_complete;
 10    
 11ExpandedSubBlockStart.gifContractedSubBlock.gif    var progress_change = function(){
 12ExpandedSubBlockStart.gifContractedSubBlock.gif        if(_progress_change){
 13            //进度改变事件
 14            //lp:已下载百分数; ppr:; ppa:已播放百分数 pt:播放时间; tt:总时间
 15            var lp = _play.network.downloadProgress;
 16            var ppr;
 17ExpandedSubBlockStart.gifContractedSubBlock.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               
 23ExpandedSubBlockStart.gifContractedSubBlock.gif                if(Number(_play.currentMedia.duration - _play.controls.currentPosition) < 0.3){
 24                     
 25ExpandedSubBlockStart.gifContractedSubBlock.gif                    if(_sound_complete){
 26                        _sound_complete();
 27                    }

 28                }

 29            }

 30        }

 31    }

 32    //改变路径(私)
 33ExpandedSubBlockStart.gifContractedSubBlock.gif    var change = function(p_url){
 34        _play.url = p_url;
 35        _play.controls.play();
 36    }

 37    
 38    //播放
 39ExpandedSubBlockStart.gifContractedSubBlock.gif    this.play = function(){
 40        _play.controls.play();
 41    }
,
 42    //改变并播放
 43ExpandedSubBlockStart.gifContractedSubBlock.gif    this.changeAndPlay = function(p_url){
 44        change(p_url);
 45        _play.controls.play();
 46    }

 47    //暂停
 48ExpandedSubBlockStart.gifContractedSubBlock.gif    this.pause = function(){
 49        _play.controls.pause(); 
 50    }
,
 51    //停止
 52ExpandedSubBlockStart.gifContractedSubBlock.gif    this.stop = function(){
 53        _play.controls.stop(); 
 54    }
,
 55    //设置音量
 56ExpandedSubBlockStart.gifContractedSubBlock.gif    this.volume = function(p_value){
 57        _play.settings.volume = p_value
 58    }
,
 59    //设置进度
 60ExpandedSubBlockStart.gifContractedSubBlock.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    //进度改变事件
 66ExpandedSubBlockStart.gifContractedSubBlock.gif    this.onProgressChange = function(p_handler){
 67        _progress_change = p_handler;
 68    }
,
 69    //播放完成事件
 70ExpandedSubBlockStart.gifContractedSubBlock.gif    this.onSoundComplete = function(p_handler){
 71        _sound_complete = p_handler;
 72    }
,
 73    //播放加载事件
 74ExpandedSubBlockStart.gifContractedSubBlock.gif    this.mPlayer = function(p_obj){
 75ExpandedSubBlockStart.gifContractedSubBlock.gif        if(p_obj){
 76            p_obj.ready();
 77        }

 78        window.setInterval(progress_change,100);
 79    }

 80}

 81
 82
 83ExpandedBlockStart.gifContractedBlock.gifvar 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    //根据序号播放歌曲
 97ExpandedSubBlockStart.gifContractedSubBlock.gif    var play_index = function(index){
 98ExpandedSubBlockStart.gifContractedSubBlock.gif        if(_datalist.length > 0){
 99            var url = _datalist[index]["filepath"];
100            _player.changeAndPlay(url);
101            _play_index = index;
102ExpandedSubBlockStart.gifContractedSubBlock.gif            if(_play_handler){
103                _play_handler(_datalist[index]);
104            }

105        }

106    }

107    
108    //播放第一首
109ExpandedSubBlockStart.gifContractedSubBlock.gif    var play_first = function(){
110ExpandedSubBlockStart.gifContractedSubBlock.gif        if(_datalist.length > 0){
111            play_index(0);
112        }

113    }

114    
115    //播放下一首
116ExpandedSubBlockStart.gifContractedSubBlock.gif    var play_next = function(){
117        var n = get_play_index_num();
118ExpandedSubBlockStart.gifContractedSubBlock.gif        if(n < _datalist.length){
119            play_index(n);
120        }

121ExpandedSubBlockStart.gifContractedSubBlock.gif        else{
122            play_index(0);
123        }

124    }

125    
126    //播放上一首
127ExpandedSubBlockStart.gifContractedSubBlock.gif    var play_previous = function(){
128        var n = get_play_index_num(true);
129ExpandedSubBlockStart.gifContractedSubBlock.gif        if(n >= 0){
130            play_index(n);
131        }

132    }

133    
134ExpandedSubBlockStart.gifContractedSubBlock.gif    var show_play_btn = function(){
135        $("#" + _btn_obj["play"]).hide();
136        $("#" + _btn_obj["pause"]).show();
137        $("#" + _btn_obj["stop"]).show();
138    }

139    
140ExpandedSubBlockStart.gifContractedSubBlock.gif    var show_pause_btn = function(){
141        $("#" + _btn_obj["play"]).show();
142        $("#" + _btn_obj["pause"]).hide();
143        $("#" + _btn_obj["stop"]).show();
144    }

145    
146ExpandedSubBlockStart.gifContractedSubBlock.gif    var show_stop_btn = function(){
147        $("#" + _btn_obj["play"]).show();
148        $("#" + _btn_obj["pause"]).hide();
149    }

150    
151    //验证Key是否存在
152ExpandedSubBlockStart.gifContractedSubBlock.gif    var check_key = function(key){
153ExpandedSubBlockStart.gifContractedSubBlock.gif        for(var i = 0; i < _datalist.length;i++){
154            var item = _datalist[i];
155ExpandedSubBlockStart.gifContractedSubBlock.gif            if(item.key == key){
156                return true;
157            }

158        }

159        return false;
160    }

161    
162ExpandedSubBlockStart.gifContractedSubBlock.gif    var get_play_index_num = function(is_pre){
163        var i = _play_index;
164ExpandedSubBlockStart.gifContractedSubBlock.gif        if(is_pre){
165ExpandedSubBlockStart.gifContractedSubBlock.gif            switch(_play_pattern){
166                case 0:
167                    break;
168                case 1:
169                    i-=1;
170                    break;
171                case 2:
172                    i-=1;
173ExpandedSubBlockStart.gifContractedSubBlock.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        }

183ExpandedSubBlockStart.gifContractedSubBlock.gif        else{
184ExpandedSubBlockStart.gifContractedSubBlock.gif            switch(_play_pattern){
185                case 0:
186                    break;
187                case 1:
188                    i+=1;
189                    break;
190                case 2:
191                    i+=1;
192ExpandedSubBlockStart.gifContractedSubBlock.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    //控制静音状态
206ExpandedSubBlockStart.gifContractedSubBlock.gif    this.control_sound = function(p_has){
207ExpandedSubBlockStart.gifContractedSubBlock.gif        if(p_has == undefined){
208            return _is_no_sound;
209        }

210        _is_no_sound = p_has;
211ExpandedSubBlockStart.gifContractedSubBlock.gif        if(_is_no_sound){
212               _player.volume(0);
213ExpandedSubBlockStart.gifContractedSubBlock.gif        }
else{
214            _player.volume(_volume);
215        }

216    }
,
217    //播放进度事件
218ExpandedSubBlockStart.gifContractedSubBlock.gif    this.on_progress_change = function(fun){
219ExpandedSubBlockStart.gifContractedSubBlock.gif        if(_progress_change){
220ExpandedSubBlockStart.gifContractedSubBlock.gif            _progress_change = function(){
221                _progress_change();
222                fun();
223            }

224        }

225ExpandedSubBlockStart.gifContractedSubBlock.gif        else{
226            _progress_change = fun;
227        }

228    }
,
229    //获得正在播放歌曲对象
230ExpandedSubBlockStart.gifContractedSubBlock.gif    this.get_active_music = function(){
231ExpandedSubBlockStart.gifContractedSubBlock.gif        if(_play_index != -1 && _datalist.length > 0){
232            return _datalist[_play_index];
233        }

234        return null;
235    }
,
236    //播放
237ExpandedSubBlockStart.gifContractedSubBlock.gif    this.play = function(p_key){
238ExpandedSubBlockStart.gifContractedSubBlock.gif        for(var i = 0; i < _datalist.length; i++){
239ExpandedSubBlockStart.gifContractedSubBlock.gif            if(p_key == _datalist[i].key){
240                play_index(i);
241            }

242        }

243    }
,
244ExpandedSubBlockStart.gifContractedSubBlock.gif    this.play_first_music = function(){
245        play_first();
246    }
,
247    //增加歌曲 必须字段 key: 音乐标识; filepath: 路径; name: 音乐名
248ExpandedSubBlockStart.gifContractedSubBlock.gif    this.add_data = function(p_key,p_name,p_filepath){
249ExpandedSubBlockStart.gifContractedSubBlock.gif        var obj = {key:p_key,filepath:p_filepath,name:p_name};
250        var result_obj = [];
251        var is_add = false;
252ExpandedSubBlockStart.gifContractedSubBlock.gif        if(!check_key(obj.key)){
253            _datalist.push(obj);
254            result_obj.push(obj);
255            is_add = true;
256        }

257ExpandedSubBlockStart.gifContractedSubBlock.gif        if(_data_change_handler){
258            _data_change_handler(is_add,result_obj);
259        }

260    }
,
261    //增加歌曲(批量)
262ExpandedSubBlockStart.gifContractedSubBlock.gif    this.add_dataarr = function(p_obj_arr){
263        var is_add = false;
264        var result_obj = [];
265ExpandedSubBlockStart.gifContractedSubBlock.gif        for(var i = 0; i < p_obj_arr.length; i++){
266ExpandedSubBlockStart.gifContractedSubBlock.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        }

272ExpandedSubBlockStart.gifContractedSubBlock.gif        if(_data_change_handler){
273            _data_change_handler(is_add,result_obj);
274        }

275        return is_add;
276    }
,
277    //删除歌曲
278ExpandedSubBlockStart.gifContractedSubBlock.gif    this.delete_data = function(p_key){
279        var index = -1;
280ExpandedSubBlockStart.gifContractedSubBlock.gif        for(var i = 0; i < _datalist.length;i++){
281            var item = _datalist[i];
282ExpandedSubBlockStart.gifContractedSubBlock.gif            if(item.key == p_key){
283                index = i;
284            }

285        }

286        var key = -1;
287ExpandedSubBlockStart.gifContractedSubBlock.gif        if(index != -1){
288            key = _datalist[index].key;
289ExpandedSubBlockStart.gifContractedSubBlock.gif            if(_data_change_handler){
290ExpandedSubBlockStart.gifContractedSubBlock.gif                _data_change_handler(true,[{key:key}],true);
291            }

292            _datalist.splice(index,1);
293        }

294ExpandedSubBlockStart.gifContractedSubBlock.gif        else{
295ExpandedSubBlockStart.gifContractedSubBlock.gif            if(_data_change_handler){
296ExpandedSubBlockStart.gifContractedSubBlock.gif                _data_change_handler(false,{key:key},true);
297            }

298        }

299    }
,
300    //绑定按钮
301ExpandedSubBlockStart.gifContractedSubBlock.gif    this.bind_btn = function(obj){
302ExpandedSubBlockStart.gifContractedSubBlock.gif        if(obj){
303            _btn_obj = obj;
304ExpandedSubBlockStart.gifContractedSubBlock.gif            $.each(_btn_obj, function(i, n){
305                var ele = $("#" + n);
306ExpandedSubBlockStart.gifContractedSubBlock.gif                if(ele == undefined){
307                    ele = $(n);
308                }

309ExpandedSubBlockStart.gifContractedSubBlock.gif                ele.click(function(){
310ExpandedSubBlockStart.gifContractedSubBlock.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    //设置音量
337ExpandedSubBlockStart.gifContractedSubBlock.gif    this.set_volume = function(p_num){
338ExpandedSubBlockStart.gifContractedSubBlock.gif        if(p_num == undefined){
339            return _volume;
340        }

341        _volume = p_num;
342        _player.volume(_is_no_sound? 0 : _volume);
343    }
,
344    //设置进度
345ExpandedSubBlockStart.gifContractedSubBlock.gif    this.set_plan = function(p_num){
346        _player.playHead(p_num);
347    }
,
348    //播放事件
349ExpandedSubBlockStart.gifContractedSubBlock.gif    this.set_play_call_back = function(p_fun){
350        _play_handler = p_fun;
351    }
,
352    //设置列表改变事件
353ExpandedSubBlockStart.gifContractedSubBlock.gif    this.set_data_change_handler = function(p_fun){
354ExpandedSubBlockStart.gifContractedSubBlock.gif        if(p_fun){
355            _data_change_handler = p_fun;
356        }

357    }
,
358    //设置播放模式
359ExpandedSubBlockStart.gifContractedSubBlock.gif    this.pattern = function(p_pattern){
360ExpandedSubBlockStart.gifContractedSubBlock.gif        if(p_pattern == undefined){
361            return _play_pattern;
362        }

363        _play_pattern = p_pattern;
364    }
,
365    //获取列表
366ExpandedSubBlockStart.gifContractedSubBlock.gif    this.get_datasource = function(){
367        return _datalist;
368    }
,
369ExpandedSubBlockStart.gifContractedSubBlock.gif    this.get_player = function(){
370        return _player;
371    }
,
372    //初始化
373ExpandedSubBlockStart.gifContractedSubBlock.gif    this.init = function(p_playId,p_play_callback){        
374ExpandedSubBlockStart.gifContractedSubBlock.gif        if($.browser.msie){
375            _player = new media_player_proxy(document.getElementById(p_playId));
376        }

377ExpandedSubBlockStart.gifContractedSubBlock.gif        else{
378            _player = $("#" + p_playId);
379        }

380ExpandedSubBlockStart.gifContractedSubBlock.gif        if(p_play_callback){
381            _play_handler = p_play_callback;
382        }

383ExpandedSubBlockStart.gifContractedSubBlock.gif        if(_progress_change){
384            _player.onProgressChange(_progress_change);
385        }

386ExpandedSubBlockStart.gifContractedSubBlock.gif        _player.onSoundComplete(function(){
387            var i = get_play_index_num();
388            play_index(i);
389        }
);
390ExpandedSubBlockStart.gifContractedSubBlock.gif        if($.browser.msie){
391ExpandedSubBlockStart.gifContractedSubBlock.gif             _player.mPlayer({
392ExpandedSubBlockStart.gifContractedSubBlock.gif                ready: function () {
393ExpandedSubBlockStart.gifContractedSubBlock.gif                    if(_play_index == -1){
394                       play_first();
395                       show_play_btn();
396                    }

397ExpandedSubBlockStart.gifContractedSubBlock.gif                    else{
398                        show_stop_btn();
399                    }

400                }

401            }
);
402        }

403ExpandedSubBlockStart.gifContractedSubBlock.gif        else{
404ExpandedSubBlockStart.gifContractedSubBlock.gif            _player.jPlayer({
405ExpandedSubBlockStart.gifContractedSubBlock.gif                ready: function () {
406ExpandedSubBlockStart.gifContractedSubBlock.gif                    if(_play_index == -1){
407                       play_first();
408                       show_play_btn();
409                    }

410ExpandedSubBlockStart.gifContractedSubBlock.gif                    else{
411                        show_stop_btn();
412                    }

413                }
,
414                swfPath: URL_STATIC+"/media/music"
415            }
);
416        }

417    }

418}

转载自原文链接, 如需删除请联系管理员。

原文链接:JQuery播放器代理--IE下支持wma格式,转载请注明来源!

0