Needed to work with sounds in a project. So i decided to make a dynamic SoundManager class to handle it all. Now i can easily load and handle the sounds i use inside a flash project. Currently this manager only plays the .mp3 format, but am looking into playing other formats aswell.
Download it at googlecode.
Maybe take a look on the wiki about it.
Or
Take a look at the class code:
View CodeACTIONSCRIPT | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 | package { import flash.media.SoundTransform; import flash.events.IOErrorEvent; import flash.events.ProgressEvent; import flash.events.Event; import flash.net.URLRequest; import flash.utils.Dictionary; import flash.media.Sound; import flash.media.SoundChannel; /** * @author RACKDOLL * @version 1.0 * @usage * SoundManager.getInstance().addSound ( "yourSoundID", "yourSoundPath" ); * SoundManager.getIinstance().playSound( "yourSoundID", yourVolume, yourRepeatCount ); * SoundManager.getInstance().stopSound ( "yourSoundID" ); */ public class SoundManager { public var sounds :Dictionary = null; public var currPlayingSounds:Dictionary = null; //*************************************************************************** // C O N S T R U C T O R //*************************************************************************** public function SoundManager() { sounds = new Dictionary(); currPlayingSounds = new Dictionary(); } // --> SINGLETON: private static var _instance:SoundManager; public static function getInstance():SoundManager { if(!_instance )_instance = new SoundManager(); return _instance; } //*************************************************************************** // A D D / R E M O V E //*************************************************************************** //------------------------------- // ADD SOUND WITH ID: //------------------------------- public function addSound( $id:String, $url:String):void { sounds[$id] = $url; } //------------------------------- // REMOVE SOUND WITH ID: //------------------------------- public function removeSound( $id:String ):void { var id:String; //1. //check current playing sounds dictionary for sound: for( id in currPlayingSounds){ if( id == $id ){ delete currPlayingSounds[id]; break; } } //2. //check all sounds dictionary for sound: for( id in sounds){ if( id == $id ){ delete sounds[id]; break; } } } //*************************************************************************** // P L A Y / S T O P //*************************************************************************** //------------------------------- // PLAY SOUND WITH ID: //------------------------------- public function playSound( $id:String, $volume:Number = 1.0, $timesToRepeat:int = 999 ):void { //first check if it alrdy excists, if so...start it! var t:SoundTransform; for( var id:String in currPlayingSounds){ if( id == $id ){ var c:SoundChannel = currPlayingSounds[id].channel as SoundChannel; var s:Sound = currPlayingSounds[id].sound as Sound; t = new SoundTransform($volume); c = s.play(0, $timesToRepeat); c.soundTransform = t; currPlayingSounds[id] = { channel: c, sound: s, volume: $volume }; return; } } //--- //If we come here, it means we have a new sound! //setup sound: var req:URLRequest = new URLRequest( sounds[$id] ); var soundFactory:Sound = new Sound(); soundFactory.addEventListener(Event.COMPLETE , onSoundFactoryComplete ); soundFactory.addEventListener(ProgressEvent.PROGRESS, onSoundFactoryProgress ); soundFactory.addEventListener(IOErrorEvent.IO_ERROR , onIOError ); //load sound: soundFactory.load( req ); //play sound: var channel:SoundChannel = new SoundChannel(); channel = soundFactory.play(0, $timesToRepeat); //set volume: t = new SoundTransform($volume); channel.soundTransform = t; //save soound in current playing sounds: currPlayingSounds[ $id ] = { channel: channel, sound: soundFactory, volume: $volume }; } //------------------------------- //STOP SOUND WITH ID: //------------------------------- public function stopSound( $id:String ):void { for( var id:String in currPlayingSounds ){ trace( id ); if( id == $id ){ SoundChannel( currPlayingSounds[id].channel ).stop(); } } } //*************************************************************************** // V O L U M E //*************************************************************************** //------------------------------- // SET ALL SOUND VOLUMES: //------------------------------- public function setGlobalVolume( $volume:Number ):void { for( var id:String in currPlayingSounds ){ var s:SoundTransform = new SoundTransform($volume); SoundChannel( currPlayingSounds[id].channel ).soundTransform = s; currPlayingSounds[id].volume = $volume; } } //------------------------------- // TOGGLE MUTE ALL SOUNDS: //------------------------------- public function muteAll( $mute:Boolean = true ):void { if( $mute ) { setGlobalVolume(0.0); } else { for( var id:String in currPlayingSounds){ var s:SoundTransform = new SoundTransform(currPlayingSounds[id].volume); SoundChannel( currPlayingSounds[id].channel ).soundTransform = s; } } } //------------------------------- // SET SOUND VOLUME BY SOUND ID: //------------------------------- public function setVolume( $id:String, $volume:Number ):void { for( var id:String in currPlayingSounds){ if( id == $id ){ var s:SoundTransform = new SoundTransform($volume); SoundChannel( currPlayingSounds[id].channel ).soundTransform = s; currPlayingSounds[id].volume = $volume; } } } //*************************************************************************** // R E A D I N G _ P R O P E R T I E S //*************************************************************************** //------------------------------- // GET SOUND CHANNEL BY SOUND ID: //------------------------------- public function getSoundChannel( $id:String ):SoundChannel { for( var id:String in currPlayingSounds){ if( id == $id ){ return SoundChannel( currPlayingSounds[id].channel ); } } throwError(ERROR_NON_EXCISTENT_CHANNEL); return null; } //------------------------------- // GET SOUND TRANSFORM BY SOUND ID: //------------------------------- public function getSoundTransform( $id:String ):SoundTransform { for( var id:String in currPlayingSounds){ if( id == $id ){ return SoundChannel( currPlayingSounds[id].channel ).soundTransform; } } throwError(ERROR_NON_EXCISTENT_TRANSFORM); return null; } //------------------------------- // GET SOUND VOLUME BY SOUND ID: //------------------------------- public function getSoundVolume( $id:String ):Number { for( var id:String in currPlayingSounds){ if( id == $id ){ return currPlayingSounds[id].volume; } } throwError(ERROR_NON_EXCISTENT_VOLUME); return NaN; } //*************************************************************************** // H A N D L E R S //*************************************************************************** public static const ERROR_NON_EXCISTENT_CHANNEL :String = "You are trying to get a non excistent soundChannel!\nPlay it first in order to assign a channel!"; public static const ERROR_NON_EXCISTENT_VOLUME :String = "You are trying to get a non excistent volume!\nPlay it first in order to assign a volume!"; public static const ERROR_NON_EXCISTENT_TRANSFORM :String = "You are trying to get a non excistent soundTransform!\nPlay it first in order to assign a transform!"; public static const ERROR_IO :String = "There is an I-O error!"; private function throwError( $error:String = "undefined_error" ):void { throw Error("*** --> SOUND WARNING: "+$error+"<-- ***"); } private function onSoundFactoryComplete(e:Event = null):void { trace("sound complete!"); } private function onSoundFactoryProgress(e:Event = null ):void { trace("sounnd progress!"); } private function onIOError(e:Event = null ):void { throwError( ERROR_IO ); } } } |
have fun!
Rackdoll

The [as3.0] SoundManager v1.0 by Script.it, unless otherwise expressly stated, is licensed under a Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 Netherlands License.
