Advertisement
Guest User

Audio.js

a guest
Jan 30th, 2014
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.     AUTHOR: Osvaldas Valutis, www.osvaldas.info
  3. */
  4.  
  5.  
  6.  
  7. ;(function( $, window, document, undefined )
  8. {
  9.     var isTouch       = 'ontouchstart' in window,
  10.         eStart        = isTouch ? 'touchstart'  : 'mousedown',
  11.         eMove         = isTouch ? 'touchmove'   : 'mousemove',
  12.         eEnd          = isTouch ? 'touchend'    : 'mouseup',
  13.         eCancel       = isTouch ? 'touchcancel' : 'mouseup',
  14.         secondsToTime = function( secs )
  15.         {
  16.             var hours = Math.floor( secs / 3600 ), minutes = Math.floor( secs % 3600 / 60 ), seconds = Math.ceil( secs % 3600 % 60 );
  17.             return ( hours == 0 ? '' : hours > 0 && hours.toString().length < 2 ? '0'+hours+':' : hours+':' ) + ( minutes.toString().length < 2 ? '0'+minutes : minutes ) + ':' + ( seconds.toString().length < 2 ? '0'+seconds : seconds );
  18.         },
  19.         canPlayType   = function( file )
  20.         {
  21.             var audioElement = document.createElement( 'audio' );
  22.             return !!( audioElement.canPlayType && audioElement.canPlayType( 'audio/' + file.split( '.' ).pop().toLowerCase() + ';' ).replace( /no/, '' ) );
  23.         };
  24.  
  25.     $.fn.audioPlayer = function( params )
  26.     {
  27.         var params      = $.extend( { classPrefix: 'audioplayer', strPlay: 'Play', strPause: 'Pause', strVolume: 'Volume' }, params ),
  28.             cssClass    = {},
  29.             cssClassSub =
  30.             {
  31.                 playPause:      'playpause',
  32.                 playing:        'playing',
  33.                 time:           'time',
  34.                 timeCurrent:    'time-current',
  35.                 timeDuration:   'time-duration',
  36.                 bar:            'bar',
  37.                 barLoaded:      'bar-loaded',
  38.                 barPlayed:      'bar-played',
  39.                 volume:         'volume',
  40.                 volumeButton:   'volume-button',
  41.                 volumeAdjust:   'volume-adjust',
  42.                 noVolume:       'novolume',
  43.                 mute:           'mute',
  44.                 mini:           'mini'
  45.             };
  46.  
  47.         for( var subName in cssClassSub )
  48.             cssClass[ subName ] = params.classPrefix + '-' + cssClassSub[ subName ];
  49.  
  50.         this.each( function()
  51.         {
  52.             if( $( this ).prop( 'tagName' ).toLowerCase() != 'audio' )
  53.                 return false;
  54.  
  55.             var $this      = $( this ),
  56.                 audioFile  = $this.attr( 'src' ),
  57.                 isAutoPlay = $this.get( 0 ).getAttribute( 'autoplay' ), isAutoPlay = isAutoPlay === '' || isAutoPlay === 'autoplay' ? true : false,
  58.                 isLoop     = $this.get( 0 ).getAttribute( 'loop' ),     isLoop     = isLoop     === '' || isLoop     === 'loop'     ? true : false,
  59.                 isSupport  = false;
  60.  
  61.             if( typeof audioFile === 'undefined' )
  62.             {
  63.                 $this.find( 'source' ).each( function()
  64.                 {
  65.                     audioFile = $( this ).attr( 'src' );
  66.                     if( typeof audioFile !== 'undefined' && canPlayType( audioFile ) )
  67.                     {
  68.                         isSupport = true;
  69.                         return false;
  70.                     }
  71.                 });
  72.             }
  73.             else if( canPlayType( audioFile ) ) isSupport = true;
  74.  
  75.             var thePlayer = $( '<div class="' + params.classPrefix + '">' + ( isSupport ? $( '<div>' ).append( $this.eq( 0 ).clone() ).html() : '<embed src="' + audioFile + '" width="0" height="0" volume="100" autostart="' + isAutoPlay.toString() +'" loop="' + isLoop.toString() + '" />' ) + '<div class="' + cssClass.playPause + '" title="' + params.strPlay + '"><a href="#">' + params.strPlay + '</a></div></div>' ),
  76.                 theAudio  = isSupport ? thePlayer.find( 'audio' ) : thePlayer.find( 'embed' ), theAudio = theAudio.get( 0 );
  77.  
  78.             if( isSupport )
  79.             {
  80.                 thePlayer.find( 'audio' ).css( { 'width': 0, 'height': 0, 'visibility': 'hidden' } );
  81.                 thePlayer.append( '<div class="' + cssClass.time + ' ' + cssClass.timeCurrent + '"></div><div class="' + cssClass.bar + '"><div class="' + cssClass.barLoaded + '"></div><div class="' + cssClass.barPlayed + '"></div></div><div class="' + cssClass.time + ' ' + cssClass.timeDuration + '"></div><div class="' + cssClass.volume + '"><div class="' + cssClass.volumeButton + '" title="' + params.strVolume + '"><a href="#">' + params.strVolume + '</a></div><div class="' + cssClass.volumeAdjust + '"><div><div></div></div></div></div>' );
  82.  
  83.                 var theBar            = thePlayer.find( '.' + cssClass.bar ),
  84.                     barPlayed         = thePlayer.find( '.' + cssClass.barPlayed ),
  85.                     barLoaded         = thePlayer.find( '.' + cssClass.barLoaded ),
  86.                     timeCurrent       = thePlayer.find( '.' + cssClass.timeCurrent ),
  87.                     timeDuration      = thePlayer.find( '.' + cssClass.timeDuration ),
  88.                     volumeButton      = thePlayer.find( '.' + cssClass.volumeButton ),
  89.                     volumeAdjuster    = thePlayer.find( '.' + cssClass.volumeAdjust + ' > div' ),
  90.                     volumeDefault     = 0,
  91.                     adjustCurrentTime = function( e )
  92.                     {
  93.                         theRealEvent         = isTouch ? e.originalEvent.touches[ 0 ] : e;
  94.                         theAudio.currentTime = Math.round( ( theAudio.duration * ( theRealEvent.pageX - theBar.offset().left ) ) / theBar.width() );
  95.                     },
  96.                     adjustVolume = function( e )
  97.                     {
  98.                         theRealEvent    = isTouch ? e.originalEvent.touches[ 0 ] : e;
  99.                         theAudio.volume = Math.abs( ( theRealEvent.pageY - ( volumeAdjuster.offset().top + volumeAdjuster.height() ) ) / volumeAdjuster.height() );
  100.                     },
  101.                     updateLoadBar = setInterval( function()
  102.                     {
  103.                         barLoaded.width( ( theAudio.buffered.end( 0 ) / theAudio.duration ) * 100 + '%' );
  104.                         if( theAudio.buffered.end( 0 ) >= theAudio.duration )
  105.                             clearInterval( updateLoadBar );
  106.                     }, 100 );
  107.  
  108.                 var volumeTestDefault = theAudio.volume, volumeTestValue = theAudio.volume = 0.111;
  109.                 if( Math.round( theAudio.volume * 1000 ) / 1000 == volumeTestValue ) theAudio.volume = volumeTestDefault;
  110.                 else thePlayer.addClass( cssClass.noVolume );
  111.  
  112.                 timeDuration.html( '&hellip;' );
  113.                 timeCurrent.text( secondsToTime( 0 ) );
  114.  
  115.                 theAudio.addEventListener( 'loadeddata', function()
  116.                 {
  117.                     timeDuration.text( secondsToTime( theAudio.duration ) );
  118.                     volumeAdjuster.find( 'div' ).height( theAudio.volume * 100 + '%' );
  119.                     volumeDefault = theAudio.volume;
  120.                 });
  121.  
  122.                 theAudio.addEventListener( 'timeupdate', function()
  123.                 {
  124.                     timeCurrent.text( secondsToTime( theAudio.currentTime ) );
  125.                     barPlayed.width( ( theAudio.currentTime / theAudio.duration ) * 100 + '%' );
  126.                 });
  127.  
  128.                 theAudio.addEventListener( 'volumechange', function()
  129.                 {
  130.                     volumeAdjuster.find( 'div' ).height( theAudio.volume * 100 + '%' );
  131.                     if( theAudio.volume > 0 && thePlayer.hasClass( cssClass.mute ) ) thePlayer.removeClass( cssClass.mute );
  132.                     if( theAudio.volume <= 0 && !thePlayer.hasClass( cssClass.mute ) ) thePlayer.addClass( cssClass.mute );
  133.                 });
  134.  
  135.                 theAudio.addEventListener( 'ended', function()
  136.                 {
  137.                     thePlayer.removeClass( cssClass.playing );
  138.                 });
  139.  
  140.                 theBar.on( eStart, function( e )
  141.                 {
  142.                     adjustCurrentTime( e );
  143.                     theBar.on( eMove, function( e ) { adjustCurrentTime( e ); } );
  144.                 })
  145.                 .on( eCancel, function()
  146.                 {
  147.                     theBar.unbind( eMove );
  148.                 });
  149.  
  150.                 volumeButton.on( 'click', function()
  151.                 {
  152.                     if( thePlayer.hasClass( cssClass.mute ) )
  153.                     {
  154.                         thePlayer.removeClass( cssClass.mute );
  155.                         theAudio.volume = volumeDefault;
  156.                     }
  157.                     else
  158.                     {
  159.                         thePlayer.addClass( cssClass.mute );
  160.                         volumeDefault = theAudio.volume;
  161.                         theAudio.volume = 0;
  162.                     }
  163.                     return false;
  164.                 });
  165.  
  166.                 volumeAdjuster.on( eStart, function( e )
  167.                 {
  168.                     adjustVolume( e );
  169.                     volumeAdjuster.on( eMove, function( e ) { adjustVolume( e ); } );
  170.                 })
  171.                 .on( eCancel, function()
  172.                 {
  173.                     volumeAdjuster.unbind( eMove );
  174.                 });
  175.             }
  176.             else thePlayer.addClass( cssClass.mini );
  177.  
  178.             if( isAutoPlay ) thePlayer.addClass( cssClass.playing );
  179.  
  180.             thePlayer.find( '.' + cssClass.playPause ).on( 'click', function()
  181.             {
  182.                 if( thePlayer.hasClass( cssClass.playing ) )
  183.                 {
  184.                     $( this ).attr( 'title', params.strPlay ).find( 'a' ).html( params.strPlay );
  185.                     thePlayer.removeClass( cssClass.playing );
  186.                     isSupport ? theAudio.pause() : theAudio.Stop();
  187.                 }
  188.                 else
  189.                 {
  190.                     $( this ).attr( 'title', params.strPause ).find( 'a' ).html( params.strPause );
  191.                     thePlayer.addClass( cssClass.playing );
  192.                     isSupport ? theAudio.play() : theAudio.Play();
  193.                 }
  194.                 return false;
  195.             });
  196.  
  197.             $this.replaceWith( thePlayer );
  198.         });
  199.         return this;
  200.     };
  201. })( jQuery, window, document );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement