Guest User

Untitled

a guest
Sep 8th, 2014
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function($,SPACES,soundManager,undefined)
  2. {$.fn.PageAudioPlayersH=function(methodOrOptions)
  3. {if(methods[methodOrOptions]){return methods[methodOrOptions].apply(this,Array.prototype.slice.call(arguments,1));}
  4. else if(typeof methodOrOptions==='object'||!methodOrOptions)
  5. {return _init.apply(this,arguments);}
  6. else{$.error('Method '+ method+' does not exist on jQuery.PageAudioPlayersH');return this;}}
  7. $.fn.PageAudioPlayersH.defaults={};
  8. var globalSoundIndex=0,documentMouseUpAdded=false,soundManager_ready=false;
  9. if(soundManager)soundManager.onready(onSoundManagerReady);
  10.  
  11.  
  12. function onSoundManagerReady(){soundManager_ready=true;}
  13.  
  14. function _init(){if(!documentMouseUpAdded){$(document).bind('mouseup.PageAudioPlayersH',playerMousEvents.onDocumentMouseUp);
  15. documentMouseUpAdded=true;}
  16. this.each(function(){var $el=$(this),data=$el.data('PageAudioPlayersH');
  17. if(!data||!data.initialized){data={soundCurrent:null,players:[],links:[],total_times:[],total_times_seconds:[],sounds_by_url:[],current_sound:null,initialized:true}
  18. $(this).data('PageAudioPlayersH',data);}});methods.gather.call(this);return this;}
  19.  
  20.  
  21. 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;}
  22. data.players=$el.find('.x-audio').get().sort(function(a,b){return $(a).offset().top- $(b).offset().top;})
  23. $.each(data.players,function(index){var $pEl=$(this);
  24. data.links.push($pEl.children('a').attr('href'));data.total_times.push($pEl.children('.x-audio-time').html());
  25. data.total_times_seconds.push($pEl.children('.x-audio-time').data('seconds'));$pEl.data('pageAudioPlayerH',{index:index})
  26. .bind('mousedown.PageAudioPlayersH',{index:index},playerMousEvents.onPlayerMouseDown)
  27. .bind('mousemove.PageAudioPlayersH',{index:index},$.proxy(playerMousEvents.onPlayerMouseMove,self))
  28. .bind('click.PageAudioPlayersH',{index:index},$.proxy(pppp,self)).children('a')
  29. .bind('click.PageAudioPlayersH',function(event){event.preventDefault();})})});},'stop':function(){this.each(function(){$(this).
  30. removeClass('x-has-audio-playing');_stop.call(this);});},'pause':function(){this.each(function(){$(this).removeClass('x-has-audio-playing');
  31. _pause.call(this);});},'play':function(index){if(soundManager_ready){this.each(function(){$(this).toggleClass('x-has-audio-playing');_play.call(this,index);});}},
  32. 'play_next':function(){this.each(function(){_playNext.call(this);});},'seek_to_fraction':function(index,fraction){this.each(function(){_seek.call(this,index,fraction);});}};
  33.  
  34.  
  35.  
  36. function _play(index){var $el=$(this),data=$el.data('PageAudioPlayersH');
  37. if(!data||!data.players[index]){return false;}
  38. var soundUrl=data.links[index]
  39. if(!soundManager.canPlayURL(soundUrl)){return false;}
  40. var sound=data.sounds_by_url[soundUrl];
  41. if(sound){
  42.           if(sound==data.current_sound){sound.togglePause();}
  43.            else{sound.togglePause();_stop.call(this);data.current_sound=sound;}}
  44. else{sound=soundManager.createSound({id:'sound_'+ globalSoundIndex++,url:soundUrl,onplay:soundEvents.
  45. play,whileloading:soundEvents.whileloading,whileplaying:soundEvents.whileplaying,onstop:soundEvents.stop,onpause:soundEvents.pause,onresume:soundEvents.resume,onfinish:soundEvents.
  46. 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;}
  47.  
  48. }
  49.  
  50.  
  51.  
  52. 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;}
  53. _play.call(this,index+ 1);}
  54. function _pause(){var $el=$(this),data=$el.data('PageAudioPlayersH');if(data&&data.current_sound&&data.current_sound.playState==1){data.current_sound.pause();}}
  55. 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);}}
  56. 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;}
  57. if(!data.current_sound||data.current_sound.playState!=1){return false;}
  58. var position=Math.floor(data.total_times_seconds[index]*1000*fraction);data.current_sound.setPosition(position);}
  59.  
  60. var $seekEl,mouseStartXPos,mouseEndXPos,playerCancelClick=false;
  61. var playerMousEvents={onPlayerLinkClick:function(event){if(!playerCancelClick){methods.play.call(this,event.data.index);}playerCancelClick=false;return false;}
  62. ,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;}}}};
  63.  
  64.  
  65.  
  66.  
  67. setTimeout(function() {play.call(this,index)}, 500)
  68. 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(){}};
  69. 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;}
  70. 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