;(function( $, window, document, undefined )
{
var istouch = 'ontouchstart' in window,
estart = istouch ? 'touchstart' : 'mousedown',
emove = istouch ? 'touchmove' : 'mousemove',
eend = istouch ? 'touchend' : 'mouseup',
ecancel = istouch ? 'touchcancel' : 'mouseup',
secondstotime = function( secs )
{
var hours = math.floor( secs / 3600 ), minutes = math.floor( secs % 3600 / 60 ), seconds = math.ceil( secs % 3600 % 60 );
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 );
},
canplaytype = function( file )
{
var audioelement = document.createelement( 'audio' );
return !!( audioelement.canplaytype && audioelement.canplaytype( 'audio/' + file.split( '.' ).pop().tolowercase() + ';' ).replace( /no/, '' ) );
};
$.fn.audioplayer = function( params )
{
var params = $.extend( { classprefix: 'audioplayer', strplay: 'play', strpause: 'pause', strvolume: 'volume' }, params ),
cssclass = {},
cssclasssub =
{
playpause: 'playpause',
playing: 'playing',
time: 'time',
timecurrent: 'time-current',
timeduration: 'time-duration',
bar: 'bar',
barloaded: 'bar-loaded',
barplayed: 'bar-played',
volume: 'volume',
volumebutton: 'volume-button',
volumeadjust: 'volume-adjust',
novolume: 'novolume',
mute: 'mute',
mini: 'mini'
};
for( var subname in cssclasssub )
cssclass[ subname ] = params.classprefix + '-' + cssclasssub[ subname ];
this.each( function()
{
if( $( this ).prop( 'tagname' ).tolowercase() != 'audio' )
return false;
var $this = $( this ),
audiofile = $this.attr( 'src' ),
isautoplay = $this.get( 0 ).getattribute( 'autoplay' ), isautoplay = isautoplay === '' || isautoplay === 'autoplay' ? true : false,
isloop = $this.get( 0 ).getattribute( 'loop' ), isloop = isloop === '' || isloop === 'loop' ? true : false,
issupport = false;
if( typeof audiofile === 'undefined' )
{
$this.find( 'source' ).each( function()
{
audiofile = $( this ).attr( 'src' );
if( typeof audiofile !== 'undefined' && canplaytype( audiofile ) )
{
issupport = true;
return false;
}
});
}
else if( canplaytype( audiofile ) ) issupport = true;
var theplayer = $( '
' + ( issupport ? $( '
' ),
theaudio = issupport ? theplayer.find( 'audio' ) : theplayer.find( 'embed' ), theaudio = theaudio.get( 0 );
if( issupport )
{
theplayer.find( 'audio' ).css( { 'width': 0, 'height': 0, 'visibility': 'hidden' } );
theplayer.append( '
' );
var thebar = theplayer.find( '.' + cssclass.bar ),
barplayed = theplayer.find( '.' + cssclass.barplayed ),
barloaded = theplayer.find( '.' + cssclass.barloaded ),
timecurrent = theplayer.find( '.' + cssclass.timecurrent ),
timeduration = theplayer.find( '.' + cssclass.timeduration ),
volumebutton = theplayer.find( '.' + cssclass.volumebutton ),
volumeadjuster = theplayer.find( '.' + cssclass.volumeadjust + ' > div' ),
volumedefault = 0,
adjustcurrenttime = function( e )
{
therealevent = istouch ? e.originalevent.touches[ 0 ] : e;
theaudio.currenttime = math.round( ( theaudio.duration * ( therealevent.pagex - thebar.offset().left ) ) / thebar.width() );
},
adjustvolume = function( e )
{
therealevent = istouch ? e.originalevent.touches[ 0 ] : e;
theaudio.volume = math.abs( ( therealevent.pagey - ( volumeadjuster.offset().top + volumeadjuster.height() ) ) / volumeadjuster.height() );
},
updateloadbar = setinterval( function()
{
//to fix the javascript bug index_size_err.
if(theaudio.buffered.length > 0){
if(theaudio.duration > 0){
barloaded.width( ( theaudio.buffered.end( 0 ) / theaudio.duration ) * 100 + '%' );
}
if( theaudio.buffered.end( 0 ) >= theaudio.duration ){
clearinterval( updateloadbar );
}
}
}, 100 );
var volumetestdefault = theaudio.volume, volumetestvalue = theaudio.volume = 0.111;
if( math.round( theaudio.volume * 1000 ) / 1000 == volumetestvalue ) theaudio.volume = volumetestdefault;
else theplayer.addclass( cssclass.novolume );
timeduration.html( '…' );
timecurrent.text( secondstotime( 0 ) );
theaudio.addeventlistener( 'loadeddata', function()
{
timeduration.text( secondstotime( theaudio.duration ) );
volumeadjuster.find( 'div' ).height( theaudio.volume * 100 + '%' );
volumedefault = theaudio.volume;
});
theaudio.addeventlistener( 'timeupdate', function()
{
timecurrent.text( secondstotime( theaudio.currenttime ) );
barplayed.width( ( theaudio.currenttime / theaudio.duration ) * 100 + '%' );
});
theaudio.addeventlistener( 'volumechange', function()
{
volumeadjuster.find( 'div' ).height( theaudio.volume * 100 + '%' );
if( theaudio.volume > 0 && theplayer.hasclass( cssclass.mute ) ) theplayer.removeclass( cssclass.mute );
if( theaudio.volume <= 0 && !theplayer.hasclass( cssclass.mute ) ) theplayer.addclass( cssclass.mute );
});
theaudio.addeventlistener( 'ended', function()
{
theplayer.removeclass( cssclass.playing );
});
thebar.on( estart, function( e )
{
adjustcurrenttime( e );
thebar.on( emove, function( e ) { adjustcurrenttime( e ); } );
})
.on( ecancel, function()
{
thebar.unbind( emove );
});
volumebutton.on( 'click', function()
{
if( theplayer.hasclass( cssclass.mute ) )
{
theplayer.removeclass( cssclass.mute );
theaudio.volume = volumedefault;
}
else
{
theplayer.addclass( cssclass.mute );
volumedefault = theaudio.volume;
theaudio.volume = 0;
}
return false;
});
volumeadjuster.on( estart, function( e )
{
adjustvolume( e );
volumeadjuster.on( emove, function( e ) { adjustvolume( e ); } );
})
.on( ecancel, function()
{
volumeadjuster.unbind( emove );
});
}
else theplayer.addclass( cssclass.mini );
if( isautoplay ) theplayer.addclass( cssclass.playing );
theplayer.find( '.' + cssclass.playpause ).on( 'click', function()
{
if( theplayer.hasclass( cssclass.playing ) )
{
$( this ).attr( 'title', params.strplay ).find( 'a' ).html( params.strplay );
theplayer.removeclass( cssclass.playing );
issupport ? theaudio.pause() : theaudio.stop();
}
else
{
$( this ).attr( 'title', params.strpause ).find( 'a' ).html( params.strpause );
theplayer.addclass( cssclass.playing );
issupport ? theaudio.play() : theaudio.play();
}
return false;
});
$this.replacewith( theplayer );
});
return this;
};
})( jquery, window, document );