| 1 | /** |
|---|
| 2 | * MP3 model class of the players MCV pattern. |
|---|
| 3 | * |
|---|
| 4 | * @author Jeroen Wijering |
|---|
| 5 | * @version 1.4 |
|---|
| 6 | **/ |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | import com.jeroenwijering.players.*; |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | class com.jeroenwijering.players.MP3Model extends AbstractModel { |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | /** array with extensions used by this model **/ |
|---|
| 16 | private var mediatypes:Array = new Array("mp3","rbs"); |
|---|
| 17 | /** Sound instance **/ |
|---|
| 18 | private var soundObject:Sound; |
|---|
| 19 | /** MovieClip to apply the sound object to **/ |
|---|
| 20 | private var soundClip:MovieClip; |
|---|
| 21 | /** interval ID of the buffer update function **/ |
|---|
| 22 | private var loadedInterval:Number; |
|---|
| 23 | /** currently loaded percentage **/ |
|---|
| 24 | private var currentLoaded:Number = 0; |
|---|
| 25 | /** interval ID of the position update function **/ |
|---|
| 26 | private var positionInterval:Number; |
|---|
| 27 | /** current state of the sound that is playing **/ |
|---|
| 28 | private var currentState:Number; |
|---|
| 29 | /** Current volume **/ |
|---|
| 30 | private var currentVolume:Number; |
|---|
| 31 | |
|---|
| 32 | |
|---|
| 33 | /** Constructor **/ |
|---|
| 34 | function MP3Model(vws:Array,ctr:AbstractController, |
|---|
| 35 | cfg:Object,fed:Object,scl:MovieClip) { |
|---|
| 36 | super(vws,ctr,cfg,fed); |
|---|
| 37 | soundClip = scl; |
|---|
| 38 | }; |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | /** Start a specific sound **/ |
|---|
| 42 | private function setStart(pos:Number) { |
|---|
| 43 | if(pos < 1 ) { |
|---|
| 44 | pos = 0; |
|---|
| 45 | } else if (pos > feeder.feed[currentItem]["duration"] - 1) { |
|---|
| 46 | pos = feeder.feed[currentItem]["duration"] - 1; |
|---|
| 47 | } |
|---|
| 48 | clearInterval(positionInterval); |
|---|
| 49 | if(feeder.feed[currentItem]["file"] != currentURL) { |
|---|
| 50 | var ref = this; |
|---|
| 51 | currentURL = feeder.feed[currentItem]["file"]; |
|---|
| 52 | soundObject = new Sound(soundClip); |
|---|
| 53 | soundObject.onSoundComplete = function() { |
|---|
| 54 | ref.currentState = 3; |
|---|
| 55 | ref.sendUpdate("state",3); |
|---|
| 56 | ref.sendCompleteEvent(); |
|---|
| 57 | }; |
|---|
| 58 | soundObject.onLoad = function(scs:Boolean) { |
|---|
| 59 | if(scs == false) { |
|---|
| 60 | ref.currentState = 3; |
|---|
| 61 | ref.sendUpdate("state",3); |
|---|
| 62 | ref.sendCompleteEvent(); |
|---|
| 63 | } |
|---|
| 64 | }; |
|---|
| 65 | soundObject.loadSound(currentURL,true); |
|---|
| 66 | soundObject.setVolume(currentVolume); |
|---|
| 67 | sendUpdate("load",0); |
|---|
| 68 | loadedInterval = setInterval(this,"updateLoaded",100); |
|---|
| 69 | } |
|---|
| 70 | if(pos != undefined) { |
|---|
| 71 | currentPosition = pos; |
|---|
| 72 | if(pos == 0) { sendUpdate("time",0,feeder.feed[currentItem]["duration"]); } |
|---|
| 73 | } |
|---|
| 74 | soundObject.start(currentPosition); |
|---|
| 75 | updatePosition(); |
|---|
| 76 | sendUpdate("size",0,0); |
|---|
| 77 | positionInterval = setInterval(this,"updatePosition",100); |
|---|
| 78 | }; |
|---|
| 79 | |
|---|
| 80 | |
|---|
| 81 | /** Read and broadcast the amount of the mp3 that's currently loaded **/ |
|---|
| 82 | private function updateLoaded() { |
|---|
| 83 | var pct:Number = Math.round(soundObject.getBytesLoaded() / |
|---|
| 84 | soundObject.getBytesTotal()*100); |
|---|
| 85 | if(isNaN(pct)) { |
|---|
| 86 | currentLoaded = 0; |
|---|
| 87 | sendUpdate("load",0); |
|---|
| 88 | } else if (pct != currentLoaded) { |
|---|
| 89 | sendUpdate("load",pct); |
|---|
| 90 | currentLoaded = pct; |
|---|
| 91 | } else if(pct >= 100) { |
|---|
| 92 | clearInterval(loadedInterval); |
|---|
| 93 | currentLoaded = 100; |
|---|
| 94 | sendUpdate("load",100); |
|---|
| 95 | } |
|---|
| 96 | }; |
|---|
| 97 | |
|---|
| 98 | |
|---|
| 99 | /** Read and broadcast the current position of the song **/ |
|---|
| 100 | private function updatePosition() { |
|---|
| 101 | var pos = soundObject.position/1000; |
|---|
| 102 | feeder.feed[currentItem]["duration"] = soundObject.duration/(10*currentLoaded); |
|---|
| 103 | if(pos == currentPosition && currentState != 1) { |
|---|
| 104 | currentState = 1; |
|---|
| 105 | sendUpdate("state",1); |
|---|
| 106 | } else if (pos != currentPosition && currentState != 2) { |
|---|
| 107 | currentState = 2; |
|---|
| 108 | sendUpdate("state",2); |
|---|
| 109 | } |
|---|
| 110 | if (pos != currentPosition) { |
|---|
| 111 | currentPosition = pos; |
|---|
| 112 | sendUpdate("time",currentPosition,feeder.feed[currentItem]["duration"]-currentPosition); |
|---|
| 113 | } |
|---|
| 114 | }; |
|---|
| 115 | |
|---|
| 116 | |
|---|
| 117 | /** Pause the sound that's currently playing. **/ |
|---|
| 118 | private function setPause(pos:Number) { |
|---|
| 119 | if(pos < 1) { |
|---|
| 120 | pos = 0; |
|---|
| 121 | } else if (pos > feeder.feed[currentItem]["duration"] - 1) { |
|---|
| 122 | pos = feeder.feed[currentItem]["duration"] - 1; |
|---|
| 123 | } |
|---|
| 124 | soundObject.stop(); |
|---|
| 125 | clearInterval(positionInterval); |
|---|
| 126 | currentState = 0; |
|---|
| 127 | sendUpdate("state",0); |
|---|
| 128 | if(pos != undefined) { |
|---|
| 129 | currentPosition = pos; |
|---|
| 130 | sendUpdate("time",currentPosition,feeder.feed[currentItem]["duration"]-currentPosition); |
|---|
| 131 | } |
|---|
| 132 | }; |
|---|
| 133 | |
|---|
| 134 | |
|---|
| 135 | /** stop and unload the sound **/ |
|---|
| 136 | private function setStop() { |
|---|
| 137 | soundObject.stop(); |
|---|
| 138 | clearInterval(positionInterval); |
|---|
| 139 | clearInterval(loadedInterval); |
|---|
| 140 | delete currentURL; |
|---|
| 141 | delete soundObject; |
|---|
| 142 | currentLoaded = 0; |
|---|
| 143 | }; |
|---|
| 144 | |
|---|
| 145 | |
|---|
| 146 | /** Set volume of the sound object. **/ |
|---|
| 147 | private function setVolume(vol:Number) { |
|---|
| 148 | super.setVolume(vol); |
|---|
| 149 | currentVolume = vol; |
|---|
| 150 | soundObject.setVolume(vol); |
|---|
| 151 | }; |
|---|
| 152 | |
|---|
| 153 | |
|---|
| 154 | } |
|---|