/ Published in: ActionScript 3
this Class can play sound from url or library in one parameter
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
package decko.sound { import flash.media.*; import flash.events.*; import flash.net.URLRequest; import flash.utils.Dictionary; import flash.utils.getDefinitionByName; import flash.system.ApplicationDomain; import flash.media.Sound; import com.greensock.TweenMax; /** * Trieda na jednoduchu manipulaciu so zvukom. Hlavny parameter soundID v prikazoch moze nadobudat * 4 rozne datove typy (String ako url, String ako kompletnu cestu ku Class, samotny objekt Class, objekt Sound). * @author FCI * @version v2 */ public class SoundMix { public static var muteFade:Number = .1; protected static var _mute:Boolean; protected static var dictionary:Dictionary = new Dictionary(); protected static var stUpdate:SoundTransform; public var sound:Sound; public var playing:Boolean; public var channel:SoundChannel; public var fade:Number = .5; public var parallel:Boolean = true; public var volume:Number = 1; public var loops:int = 0; public var soundComplete:Function = null; /** * Spusti prehravanie zvuku * @param soundID tento parameter moze prijimat 4 rozne typy dat: Class vo forme retazca s kompletnym mennym priestorom, priamo ako Class, objekt Sound a url cestu k mp3 suboru * @param vars parallel: true, volume: 1, loops: 0, fade: .5, onComplete: null */ public function SoundMix(soundID:Object, vars:Object = null) { if (vars == null) { vars = { parallel: 1, volume: 1, loops: 1, fade: 1, soundComplete: 1 }; } vars.hasOwnProperty("parallel") ? parallel = vars.parallel : true; vars.hasOwnProperty("volume") ? volume = vars.volume : null; vars.hasOwnProperty("loops") ? loops = vars.loops : null; vars.hasOwnProperty("fade") ? fade = vars.fade : null; vars.hasOwnProperty("onComplete") ? soundComplete = vars.onComplete : null; if (parallel || !(dictionary[soundID] && dictionary[soundID].playing)) { if (dictionary[soundID] && dictionary[soundID].playing){ TweenMax.killTweensOf(dictionary[soundID].channel); } if(soundID is String){ if (!ApplicationDomain.currentDomain.hasDefinition(String(soundID))) { var reg:RegExp = /\.mp3/i; if(reg.test(String(soundID))){ sound = new Sound(new URLRequest(String(soundID))); sound.addEventListener(IOErrorEvent.IO_ERROR, soundIOError) }else{ throw new Error("Error url or soundID!"); } } else { var soundClass:Class; soundClass = getDefinitionByName(String(soundID)) as Class; sound = new soundClass(); } }else if (soundID is Class) { sound = new soundID(); }else if(soundID is Sound){ sound = Sound(soundID); } channel = sound.play(0, loops, new SoundTransform(0)); channel.addEventListener(Event.SOUND_COMPLETE, playComplete); TweenMax.to(channel, fade, {volume: volume}); playing = true; dictionary[soundID] = this; } } private function soundIOError(e:IOErrorEvent):void { throw new Error("Error url in soundID!"); } public function soundStop():void { channel.stop(); channel.removeEventListener(Event.SOUND_COMPLETE, playComplete); playing = false; } protected function playComplete(e:Event):void { playing = false; soundComplete == null ? soundComplete() : null; } // STATIC FUNCTION // ---------------------------------------------------------------------------------------------------------- /** * Spusti prehravanie zvuku * @param soundID tento identifikator moze prijimat 4 rozne typy dat: Class vo forme retazca s kompletnym mennym priestorom, priamo ako Class, objekt Sound a url cestu k mp3 suboru * @param vars parallel: true, volume: 1, loops: 0, fade: .5, onComplete: null */ public static function play(soundID:Object, vars:Object = null):SoundMix { return new SoundMix(soundID, vars); } /** * @param soundID identifikator zvuku */ public static function stop(soundID:Object):void { if (dictionary[soundID] && dictionary[soundID].playing) { TweenMax.to(dictionary[soundID].channel, dictionary[soundID].fade, {volume: 0, onComplete: dictionary[soundID].soundStop}); } } /** * overi ci sa prave prehrava skladba s konkretnym soundID * @param soundID nazov CLASS s kompletnou cestou alebo url adresa */ public static function isPlaying(soundID:Object):Boolean { return dictionary[soundID].playing; } public static function get mute():Boolean { return _mute; } public static function set mute(value:Boolean):void { _mute = value; var volumeInit:Number = int(_mute); var volume:Number = int(!_mute); stUpdate = new SoundTransform(volumeInit); TweenMax.to(stUpdate, muteFade, { volume: volume, onUpdate: updateVolume } ); } protected static function updateVolume():void { SoundMixer.soundTransform = stUpdate; } } }