Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (function($,SPACES,soundManager,undefined)
- {$.fn.PageAudioPlayersH=function(methodOrOptions)
- {if(methods[methodOrOptions]){return methods[methodOrOptions].apply(this,Array.prototype.slice.call(arguments,1));}
- else if(typeof methodOrOptions==='object'||!methodOrOptions)
- {return _init.apply(this,arguments);}
- else{$.error('Method '+ method+' does not exist on jQuery.PageAudioPlayersH');return this;}}
- $.fn.PageAudioPlayersH.defaults={};
- var globalSoundIndex=0,documentMouseUpAdded=false,soundManager_ready=false;
- if(soundManager)soundManager.onready(onSoundManagerReady);
- function onSoundManagerReady(){soundManager_ready=true;}
- function _init(){if(!documentMouseUpAdded){$(document).bind('mouseup.PageAudioPlayersH',playerMousEvents.onDocumentMouseUp);
- documentMouseUpAdded=true;}
- this.each(function(){var $el=$(this),data=$el.data('PageAudioPlayersH');
- if(!data||!data.initialized){data={soundCurrent:null,players:[],links:[],total_times:[],total_times_seconds:[],sounds_by_url:[],current_sound:null,initialized:true}
- $(this).data('PageAudioPlayersH',data);}});methods.gather.call(this);return this;}
- var methods={'gather':function(){var self=this;this.each(function(){var $el=$(this),data=$el.data('PageAudioPlayersH');if(data.players&&data.players.length>0){return true;}
- data.players=$el.find('.x-audio').get().sort(function(a,b){return $(a).offset().top- $(b).offset().top;})
- $.each(data.players,function(index){var $pEl=$(this);
- data.links.push($pEl.children('a').attr('href'));data.total_times.push($pEl.children('.x-audio-time').html());
- data.total_times_seconds.push($pEl.children('.x-audio-time').data('seconds'));$pEl.data('pageAudioPlayerH',{index:index})
- .bind('mousedown.PageAudioPlayersH',{index:index},playerMousEvents.onPlayerMouseDown)
- .bind('mousemove.PageAudioPlayersH',{index:index},$.proxy(playerMousEvents.onPlayerMouseMove,self))
- .bind('click.PageAudioPlayersH',{index:index},$.proxy(pppp,self)).children('a')
- .bind('click.PageAudioPlayersH',function(event){event.preventDefault();})})});},'stop':function(){this.each(function(){$(this).
- removeClass('x-has-audio-playing');_stop.call(this);});},'pause':function(){this.each(function(){$(this).removeClass('x-has-audio-playing');
- _pause.call(this);});},'play':function(index){if(soundManager_ready){this.each(function(){$(this).toggleClass('x-has-audio-playing');_play.call(this,index);});}},
- 'play_next':function(){this.each(function(){_playNext.call(this);});},'seek_to_fraction':function(index,fraction){this.each(function(){_seek.call(this,index,fraction);});}};
- function _play(index){var $el=$(this),data=$el.data('PageAudioPlayersH');
- if(!data||!data.players[index]){return false;}
- var soundUrl=data.links[index]
- if(!soundManager.canPlayURL(soundUrl)){return false;}
- var sound=data.sounds_by_url[soundUrl];
- if(sound){
- if(sound==data.current_sound){sound.togglePause();}
- else{sound.togglePause();_stop.call(this);data.current_sound=sound;}}
- else{sound=soundManager.createSound({id:'sound_'+ globalSoundIndex++,url:soundUrl,onplay:soundEvents.
- play,whileloading:soundEvents.whileloading,whileplaying:soundEvents.whileplaying,onstop:soundEvents.stop,onpause:soundEvents.pause,onresume:soundEvents.resume,onfinish:soundEvents.
- finish,onid3:soundEvents.id3,onmetadata:soundEvents.metadata});sound._data={index:index,players_container:this,player_element:data.players[index],time_element:$(data.players[index]).children('.x-audio-time').get(0),loading_element:$(data.players[index]).children('.x-audio-loading').get(0),progress_element:$(data.players[index]).children('.x-audio-progress').get(0),total_time:data.total_times[index],total_time_seconds:data.total_times_seconds[index]};data.sounds_by_url[soundUrl]=sound;_stop.call(this);sound.play();data.current_sound=sound;}
- }
- function _playNext(){var $el=$(this),data=$el.data('PageAudioPlayersH'),index;if(data.current_sound&&data.current_sound._data){index=data.current_sound._data.index;}else{index=0;}
- _play.call(this,index+ 1);}
- function _pause(){var $el=$(this),data=$el.data('PageAudioPlayersH');if(data&&data.current_sound&&data.current_sound.playState==1){data.current_sound.pause();}}
- function _stop(){var $el=$(this),data=$el.data('PageAudioPlayersH');if(data&&data.current_sound){soundManager.stop(data.current_sound.sID);soundManager.unload(data.current_sound.sID);}}
- function _seek(index,fraction){var $el=$(this),data=$el.data('PageAudioPlayersH');if(!data||!data.players[index]||!$(data.players[index]).hasClass('x-audio-playing')){return false;}
- if(!data.current_sound||data.current_sound.playState!=1){return false;}
- var position=Math.floor(data.total_times_seconds[index]*1000*fraction);data.current_sound.setPosition(position);}
- var $seekEl,mouseStartXPos,mouseEndXPos,playerCancelClick=false;
- var playerMousEvents={onPlayerLinkClick:function(event){if(!playerCancelClick){methods.play.call(this,event.data.index);}playerCancelClick=false;return false;}
- ,onPlayerMouseDown:function(event){var $aEl=$(this);if($aEl.hasClass('x-audio-playing')){$seekEl=$aEl;mouseStartXPos=mouseEndXPos=event.pageX;}},onPlayerMouseMove:function(event){if($seekEl&&$seekEl.length>0){mouseEndXPos=event.pageX;if(Math.abs(mouseEndXPos- mouseStartXPos)>5){var fraction=(event.pageX- $seekEl.offset().left)/$seekEl.width();methods.seek_to_fraction.call(this,event.data.index,fraction);}}},onDocumentMouseUp:function(event){if($seekEl&&$seekEl.length>0){$seekEl=null;if(Math.abs(mouseEndXPos- mouseStartXPos)>5){playerCancelClick=true;setTimeout(function(){playerCancelClick=false;},20);return false;}}}};
- setTimeout(function() {play.call(this,index)}, 500)
- var soundEvents={'play':function ppplay(){$(this._data.player_element).addClass('x-audio-playing');},'stop':function(){var $el=$(this._data.player_element).removeClass('x-audio-playing');$(this._data.time_element).html(this._data.total_time);$(this._data.progress_element).css('width',0);},'pause':function(){$(this._data.player_element).removeClass('x-audio-playing');},'resume':function(){$(this._data.player_element).addClass('x-audio-playing');},'finish':function(){var $el=$(this._data.player_element).removeClass('x-audio-playing');$(this._data.time_element).html(this._data.total_time);_playNext.call(this._data.players_container);},'whileloading':function(){$(this._data.loading_element).css('width',(this.bytesLoaded/this.bytesTotal*100)+'%');},'whileplaying':function(){var duration=_getDuration(this),time_left=_secondsToTime(Math.round((duration- this.position)/1000)),str='';if(time_left.h>0)str+=(time_left.h>9?time_left.h:('0'+ time_left.h))+':';str+=(time_left.m>9?time_left.m:('0'+ time_left.m))+':';str+=(time_left.s>9?time_left.s:('0'+ time_left.s));$(this._data.time_element).html(str);$(this._data.progress_element).css('width',(this.position/duration*100)+'%');},'id3':function(){}};
- function _secondsToTime(secs){var hours=Math.floor(secs/(60*60));var divisor_for_minutes=secs%(60*60);var minutes=Math.floor(divisor_for_minutes/60);var divisor_for_seconds=divisor_for_minutes%60;var seconds=Math.ceil(divisor_for_seconds);var obj={"h":hours,"m":minutes,"s":seconds};return obj;}
- function _getDuration(sound){if(sound.loaded){return sound.duration;}else{return sound._data.total_time_seconds*1000;}};})(jQueryLatest,window.SPACES,window.soundManager);
Advertisement
Add Comment
Please, Sign In to add comment