00001 import gugga.events.EventDispatcher;
00002 import mx.utils.Delegate;
00003
00004 import gugga.common.IProgressiveTask;
00005 import gugga.common.ProgressEventInfo;
00006 import gugga.debug.Assertion;
00007
00008 [Event("start")]
00009 [Event("progress")]
00010 [Event("interrupted")]
00011 [Event("completed")]
00012
00016 class gugga.components.SoundLoader extends EventDispatcher implements IProgressiveTask
00017 {
00018 private var PROGRESS_INTERVAL:Number = 100;
00019 private var mIntervalID:Number;
00020 private var mSound:Sound;
00021
00022 private var mIsRunning:Boolean;
00023 private var mInterrupted:Boolean;
00024
00025 private var mPercentsLoaded:Number;
00026
00027 private var mSoundPath:String;
00028 public function get SoundPath():String { return mSoundPath; }
00029 public function set SoundPath(aValue:String):Void { mSoundPath = aValue; }
00030
00031 private var mIsStreaming:Boolean;
00032 public function get IsStreaming():Boolean { return mIsStreaming; }
00033 public function set IsStreaming(aValue:Boolean):Void { mIsStreaming = aValue; }
00034
00035
00036 public function SoundLoader(aSound:Sound, aSoundPath:String, aIsStreaming:Boolean)
00037 {
00038 mSound = aSound;
00039 mSoundPath = aSoundPath;
00040 mIsStreaming = aIsStreaming;
00041 mIsRunning = false;
00042 mPercentsLoaded = 0;
00043 }
00044
00045 public function start() : Void
00046 {
00047 reset();
00048
00049 mIsRunning = true;
00050 mInterrupted = false;
00051 mPercentsLoaded = 0;
00052
00053 dispatchEvent({type:"start", target:this});
00054
00055 mSound.onLoad = Delegate.create(this, onSoundLoaded);
00056 mIntervalID = setInterval(this, "onSoundProgress", PROGRESS_INTERVAL);
00057
00058 mSound.loadSound(mSoundPath, mIsStreaming);
00059 }
00060
00061 public function isImmediatelyInterruptable() : Boolean
00062 {
00063 return false;
00064 }
00065
00066 public function interrupt() : Void
00067 {
00068 reset();
00069 mInterrupted = true;
00070 }
00071
00072 private function reset()
00073 {
00074 mSound.stop();
00075 clearInterval(mIntervalID);
00076 mSound.onLoad = null;
00077 }
00078
00079 private function onSoundProgress(ev) : Void
00080 {
00081 var bytesLoaded:Number = mSound.getBytesLoaded();
00082 var bytesTotal:Number = mSound.getBytesTotal();
00083 mPercentsLoaded = (bytesLoaded / bytesTotal) * 100;
00084
00085 dispatchEvent(new ProgressEventInfo(this, bytesTotal, bytesLoaded, mPercentsLoaded));
00086 }
00087
00088 private function onSoundLoaded(aSuccess:Boolean)
00089 {
00090 reset();
00091
00092 mPercentsLoaded = 100;
00093 mIsRunning = false;
00094
00095 Assertion.failIfFalse(
00096 aSuccess,
00097 "File not loaded", this, arguments);
00098
00099 if(mInterrupted)
00100 {
00101 dispatchEvent({type:"interrupted", target:this});
00102 }
00103 else
00104 {
00105 dispatchEvent({type:"completed", target:this});
00106 }
00107 }
00108
00109 public function getProgress() : Number
00110 {
00111 return mPercentsLoaded;
00112 }
00113
00114 public function isRunning() : Boolean
00115 {
00116 return mIsRunning;
00117 }
00118
00119 }