/**
* VERSION: 1.881
* DATE: 2011-08-15
* AS3
* UPDATES AND DOCS AT: http://www.greensock.com/loadermax/
**/
package com.greensock.loading {
import com.greensock.events.LoaderEvent;
import com.greensock.loading.core.LoaderItem;
import flash.display.Shape;
import flash.events.Event;
import flash.events.ProgressEvent;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.media.SoundLoaderContext;
import flash.media.SoundTransform;
/**
* Loads an MP3 audio file and also provides convenient playback methods
* and properties like pauseSound(), playSound(), gotoSoundTime(), playProgress, volume,
* soundPaused, duration,
and soundTime
. An MP3Loader will dispatch useful events
* like SOUND_COMPLETE, SOUND_PAUSE, SOUND_PLAY
, and PLAY_PROGRESS
in addition
* to the typical loader events, making it easy to hook up your own control interface. It packs a
* surprising amount of functionality into a very small amount of kb.
*
* OPTIONAL VARS PROPERTIES
* The following special properties can be passed into the MP3Loader constructor via its vars
* parameter which can be either a generic object or an MP3LoaderVars
object:
*
find()
method or traced at any time. Each loader's name should be unique. If you don't define one, a unique name will be created automatically, like "loader21".autoPlay
to false
.playSound()
or when autoPlay
is true
(default volume is 1).bytesLoaded
to wait for before the LoaderEvent.INIT
event is dispatched - the higher the number the more accurate the duration
estimate will be when the INIT event is dispatched (the default value is 102400 which is 100k). The MP3's duration cannot be determined with 100% accuracy until it has completely loaded, but it is estimated with more and more accuracy as the file loads.alternateURL
, the loader will initially try to load from its original url
and if it fails, it will automatically (and permanently) change the loader's url
to the alternateURL
and try again. Think of it as a fallback or backup url
. It is perfectly acceptable to use the same alternateURL
for multiple loaders (maybe a default image for various ImageLoaders for example).SoundLoaderContext
object. The default context is null. See Adobe's SoundLoaderContext documentation for details.noCache
is true
, a "gsCacheBusterID" parameter will be appended to the url with a random set of numbers to prevent caching (don't worry, this info is ignored when you getLoader()
or getContent()
by url and when you're running locally)bytesTotal
is set to the estimatedBytes
value (or LoaderMax.defaultEstimatedBytes
if one isn't defined). Then, when the loader begins loading and it can accurately determine the bytesTotal, it will do so. Setting estimatedBytes
is optional, but the more accurate the value, the more accurate your loaders' overall progress will be initially. If the loader will be inserted into a LoaderMax instance (for queue management), its auditSize
feature can attempt to automatically determine the bytesTotal
at runtime (there is a slight performance penalty for this, however - see LoaderMax's documentation for details).requireWithRoot
property to your swf's root
. For example, var loader:MP3Loader = new MP3Loader("audio.mp3", {name:"audio", requireWithRoot:this.root});
allowMalformedURL:true
. For example, if your URL has duplicate variables in the query string like http://www.greensock.com/?c=S&c=SE&c=SW
, it is technically considered a malformed URL and a URLVariables object can't properly contain all the duplicates, so in this case you'd want to set allowMalformedURL
to true
.autoDispose
is true
, the loader will be disposed immediately after it completes (it calls the dispose()
method internally after dispatching its COMPLETE
event). This will remove any listeners that were defined in the vars object (like onComplete, onProgress, onError, onInit). Once a loader is disposed, it can no longer be found with LoaderMax.getLoader()
or LoaderMax.getContent()
- it is essentially destroyed but its content is not unloaded (you must call unload()
or dispose(true)
to unload its content). The default autoDispose
value is false
.
*
* LoaderEvent.OPEN
events which are dispatched when the loader begins loading. Make sure your onOpen function accepts a single parameter of type LoaderEvent
(com.greensock.events.LoaderEvent
).Event.INIT
events which will be dispatched when the bytesLoaded
exceeds the initThreshold
(100k by default) and the MP3 has streamed enough of its content to identify the ID3 meta data. Make sure your onInit function accepts a single parameter of type LoaderEvent
(com.greensock.events.LoaderEvent
).LoaderEvent.PROGRESS
events which are dispatched whenever the bytesLoaded
changes. Make sure your onProgress function accepts a single parameter of type LoaderEvent
(com.greensock.events.LoaderEvent
). You can use the LoaderEvent's target.progress
to get the loader's progress value or use its target.bytesLoaded
and target.bytesTotal
.LoaderEvent.COMPLETE
events which are dispatched when the loader has finished loading successfully. Make sure your onComplete function accepts a single parameter of type LoaderEvent
(com.greensock.events.LoaderEvent
).LoaderEvent.CANCEL
events which are dispatched when loading is aborted due to either a failure or because another loader was prioritized or cancel()
was manually called. Make sure your onCancel function accepts a single parameter of type LoaderEvent
(com.greensock.events.LoaderEvent
).LoaderEvent.ERROR
events which are dispatched whenever the loader experiences an error (typically an IO_ERROR or SECURITY_ERROR). An error doesn't necessarily mean the loader failed, however - to listen for when a loader fails, use the onFail
special property. Make sure your onError function accepts a single parameter of type LoaderEvent
(com.greensock.events.LoaderEvent
).LoaderEvent.FAIL
events which are dispatched whenever the loader fails and its status
changes to LoaderStatus.FAILED
. Make sure your onFail function accepts a single parameter of type LoaderEvent
(com.greensock.events.LoaderEvent
).LoaderEvent.IO_ERROR
events which will also call the onError handler, so you can use that as more of a catch-all whereas onIOError
is specifically for LoaderEvent.IO_ERROR events. Make sure your onIOError function accepts a single parameter of type LoaderEvent
(com.greensock.events.LoaderEvent
).MP3LoaderVars
instance
* instead of a generic object to define your vars
is a bit more verbose but provides
* code hinting and improved debugging because it enforces strict data typing. Use whichever one you prefer.content
data type: flash.media.Sound
Sound
* object that MP3Loader employs must get recreated internally anytime the MP3Loader is unloaded or its loading
* is cancelled, so it is best to access the content
after the COMPLETE
* event has been dispatched. Otherwise, if you store a reference to the MP3Loader's content
* before or during a load and it gets cancelled or unloaded for some reason, the Sound
object
* won't be the one into which the MP3 is eventually loaded.bytesLoaded
to wait for before the LoaderEvent.INIT
event is dispatched - the higher the number the more accurate the duration
estimate will be when the INIT event is dispatched (the default value is 102400 which is 100k). The MP3's duration cannot be determined with 100% accuracy until it has completely loaded, but it is estimated with more and more accuracy as the file loads. **/
public var initThreshold:uint;
/** The SoundChannel object that results from the most recent playSound()
call (or when autoPlay
is true
in the constructor's vars
parameter). Typically there isn't much reason to use this directly. Instead, use the MP3Loader's controls like playSound(), pauseSound(), gotoSoundTime(), playProgress, duration, soundTime
, etc. **/
public var channel:SoundChannel;
/**
* Constructor.
*
* @param urlOrRequest The url (String
) or URLRequest
from which the loader should get its content
* @param vars An object containing optional configuration details. For example: new MP3Loader("mp3/audio.mp3", {name:"audio", autoPlay:true, onComplete:completeHandler, onProgress:progressHandler})
.vars
parameter
* which can be either a generic object or an MP3LoaderVars
object:find()
method or traced at any time. Each loader's name should be unique. If you don't define one, a unique name will be created automatically, like "loader21".autoPlay
to false
.bytesLoaded
to wait for before the LoaderEvent.INIT
event is dispatched - the higher the number the more accurate the duration
estimate will be when the INIT event is dispatched (the default value is 102400 which is 100k). The MP3's duration cannot be determined with 100% accuracy until it has completely loaded, but it is estimated with more and more accuracy as the file loads.alternateURL
, the loader will initially try to load from its original url
and if it fails, it will automatically (and permanently) change the loader's url
to the alternateURL
and try again. Think of it as a fallback or backup url
. It is perfectly acceptable to use the same alternateURL
for multiple loaders (maybe a default image for various ImageLoaders for example).SoundLoaderContext
object. The default context is null. See Adobe's SoundLoaderContext documentation for details.noCache
is true
, a "gsCacheBusterID" parameter will be appended to the url with a random set of numbers to prevent caching (don't worry, this info is ignored when you getLoader()
or getContent()
by url and when you're running locally)bytesTotal
is set to the estimatedBytes
value (or LoaderMax.defaultEstimatedBytes
if one isn't defined). Then, when the loader begins loading and it can accurately determine the bytesTotal, it will do so. Setting estimatedBytes
is optional, but the more accurate the value, the more accurate your loaders' overall progress will be initially. If the loader will be inserted into a LoaderMax instance (for queue management), its auditSize
feature can attempt to automatically determine the bytesTotal
at runtime (there is a slight performance penalty for this, however - see LoaderMax's documentation for details).requireWithRoot
property to your swf's root
. For example, var loader:MP3Loader = new MP3Loader("audio.mp3", {name:"audio", requireWithRoot:this.root});
allowMalformedURL:true
. For example, if your URL has duplicate variables in the query string like http://www.greensock.com/?c=S&c=SE&c=SW
, it is technically considered a malformed URL and a URLVariables object can't properly contain all the duplicates, so in this case you'd want to set allowMalformedURL
to true
.autoDispose
is true
, the loader will be disposed immediately after it completes (it calls the dispose()
method internally after dispatching its COMPLETE
event). This will remove any listeners that were defined in the vars object (like onComplete, onProgress, onError, onInit). Once a loader is disposed, it can no longer be found with LoaderMax.getLoader()
or LoaderMax.getContent()
- it is essentially destroyed but its content is not unloaded (you must call unload()
or dispose(true)
to unload its content). The default autoDispose
value is false
.
*
* LoaderEvent.OPEN
events which are dispatched when the loader begins loading. Make sure your onOpen function accepts a single parameter of type LoaderEvent
(com.greensock.events.LoaderEvent
).Event.INIT
events which will be dispatched when the bytesLoaded
exceeds the initThreshold
(100k by default) and the MP3 has streamed enough of its content to identify the ID3 meta data. Make sure your onInit function accepts a single parameter of type LoaderEvent
(com.greensock.events.LoaderEvent
).LoaderEvent.PROGRESS
events which are dispatched whenever the bytesLoaded
changes. Make sure your onProgress function accepts a single parameter of type LoaderEvent
(com.greensock.events.LoaderEvent
). You can use the LoaderEvent's target.progress
to get the loader's progress value or use its target.bytesLoaded
and target.bytesTotal
.LoaderEvent.COMPLETE
events which are dispatched when the loader has finished loading successfully. Make sure your onComplete function accepts a single parameter of type LoaderEvent
(com.greensock.events.LoaderEvent
).LoaderEvent.CANCEL
events which are dispatched when loading is aborted due to either a failure or because another loader was prioritized or cancel()
was manually called. Make sure your onCancel function accepts a single parameter of type LoaderEvent
(com.greensock.events.LoaderEvent
).LoaderEvent.ERROR
events which are dispatched whenever the loader experiences an error (typically an IO_ERROR or SECURITY_ERROR). An error doesn't necessarily mean the loader failed, however - to listen for when a loader fails, use the onFail
special property. Make sure your onError function accepts a single parameter of type LoaderEvent
(com.greensock.events.LoaderEvent
).LoaderEvent.FAIL
events which are dispatched whenever the loader fails and its status
changes to LoaderStatus.FAILED
. Make sure your onFail function accepts a single parameter of type LoaderEvent
(com.greensock.events.LoaderEvent
).LoaderEvent.IO_ERROR
events which will also call the onError handler, so you can use that as more of a catch-all whereas onIOError
is specifically for LoaderEvent.IO_ERROR events. Make sure your onIOError function accepts a single parameter of type LoaderEvent
(com.greensock.events.LoaderEvent
).
*
* loader.gotoSoundTime(3, true);
*
* @param time The time (in seconds, offset from the very beginning) at which to place the virtual playhead in the sound.
* @param forcePlay If true
, the sound will resume playback immediately after seeking to the new position.
* @param resetRepeatCount If the MP3Loader has a non-zero repeat
value (meaning it loops/repeats at least once), setting resetRepeatCount
to true
will cause it to act like this is the first time through (no repeats yet). For example, if the MP3Loader had a repeat
value of 3 and it already repeated twice when gotoSoundTime()
was called, it would act like it forgot that it repeated twice already.
* @see #pauseSound()
* @see #playSound()
* @see #soundTime
* @see #playProgress
**/
public function gotoSoundTime(time:Number, forcePlay:Boolean=false, resetRepeatCount:Boolean=true):void {
if (time > _duration) {
time = _duration;
}
_position = time * 1000;
_soundComplete = false;
if (resetRepeatCount) {
_repeatCount = 0;
}
if (!_soundPaused || forcePlay) {
_playSound(_position);
if (_soundPaused) {
_soundPaused = false;
dispatchEvent(new LoaderEvent(SOUND_PLAY, this));
}
}
}
protected function _playSound(position:Number):void {
if (this.channel != null) {
this.channel.removeEventListener(Event.SOUND_COMPLETE, _soundCompleteHandler);
this.channel.stop();
}
_position = position;
this.channel = _sound.play(_position, 1, _soundTransform);
if (this.channel != null) { //if the device doesn't have a sound card or sound capabilities, this.channel will be null!
this.channel.addEventListener(Event.SOUND_COMPLETE, _soundCompleteHandler);
_shape.addEventListener(Event.ENTER_FRAME, _enterFrameHandler, false, 0, true);
}
}
//---- EVENT HANDLERS ------------------------------------------------------------------------------------
/** @private **/
protected function _id3Handler(event:Event):void {
if (_sound.bytesLoaded > this.initThreshold) {
_initPhase = 1;
dispatchEvent(new LoaderEvent(LoaderEvent.INIT, this));
} else {
_initPhase = 0;
}
}
/** @private **/
override protected function _progressHandler(event:Event):void {
if (_initPhase == 0 && _sound.bytesLoaded > this.initThreshold) {
_initPhase = 1;
dispatchEvent(new LoaderEvent(LoaderEvent.INIT, this));
}
super._progressHandler(event);
}
/** @private **/
protected function _soundCompleteHandler(event:Event):void {
if (uint(this.vars.repeat) > _repeatCount || int(this.vars.repeat) == -1) {
_repeatCount++;
_playSound(0);
} else {
_repeatCount = 0;
_soundComplete = true;
this.soundPaused = true;
_position = _duration * 1000;
_enterFrameHandler(null);
dispatchEvent(new LoaderEvent(SOUND_COMPLETE, this));
}
}
/** @private **/
protected function _enterFrameHandler(event:Event):void {
if (_dispatchPlayProgress) {
dispatchEvent(new LoaderEvent(PLAY_PROGRESS, this));
}
}
/** @inheritDoc **/
override public function addEventListener(type:String, listener:Function, useCapture:Boolean=false, priority:int=0, useWeakReference:Boolean=false):void {
if (type == PLAY_PROGRESS) {
_dispatchPlayProgress = true;
}
super.addEventListener(type, listener, useCapture, priority, useWeakReference);
}
/** @private **/
override protected function _completeHandler(event:Event=null):void {
_duration = _sound.length / 1000;
if (_initPhase != 1) {
_initPhase = 1;
dispatchEvent(new LoaderEvent(LoaderEvent.INIT, this));
}
super._completeHandler(event);
}
//---- GETTERS / SETTERS -------------------------------------------------------------------------
/** The playback status of the sound: true
if the sound's playback is paused, false
if it isn't. **/
public function get soundPaused():Boolean {
return _soundPaused;
}
public function set soundPaused(value:Boolean):void {
var changed:Boolean = Boolean(value != _soundPaused);
_soundPaused = value;
if (!changed) {
return;
}
if (_soundPaused) {
if (this.channel != null) {
_position = this.channel.position;
this.channel.removeEventListener(Event.SOUND_COMPLETE, _soundCompleteHandler);
_shape.removeEventListener(Event.ENTER_FRAME, _enterFrameHandler);
this.channel.stop();
}
} else {
_playSound(_position);
if (this.channel == null) { //if the device doesn't have a sound card or sound capabilities, this.channel will be null!
return; //so that no event is dispatched
}
}
dispatchEvent(new LoaderEvent(((_soundPaused) ? SOUND_PAUSE : SOUND_PLAY), this));
}
/** A value between 0 and 1 describing the playback progress where 0 means the virtual playhead is at the very beginning of the sound, 0.5 means it is at the halfway point and 1 means it is at the end of the sound. **/
public function get playProgress():Number {
return (_soundComplete) ? 1 : (this.soundTime / this.duration);
}
public function set playProgress(value:Number):void {
if (this.duration != 0) {
gotoSoundTime((value * _duration), !_soundPaused);
}
}
/** The volume of the sound (a value between 0 and 1). **/
public function get volume():Number {
return _soundTransform.volume;
}
public function set volume(value:Number):void {
_soundTransform.volume = value;
if (this.channel != null) {
this.channel.soundTransform = _soundTransform;
}
}
/** The time (in seconds) at which the virtual playhead is positioned on the sound. For example, if the virtual playhead is currently at the 3-second position (3 seconds from the beginning), this value would be 3. **/
public function get soundTime():Number {
return (!_soundPaused && this.channel != null) ? this.channel.position / 1000 : _position / 1000;
}
public function set soundTime(value:Number):void {
gotoSoundTime(value, !_soundPaused);
}
/** The duration (in seconds) of the sound. This value cannot be determined with 100% accuracy until the file has completely loaded, but it is estimated with more and more accuracy as the file loads. **/
public function get duration():Number {
if (_sound.bytesLoaded < _sound.bytesTotal) {
_duration = (_sound.length / 1000) / (_sound.bytesLoaded / _sound.bytesTotal);
}
return _duration;
}
}
}