Return to Snippet

Revision: 60422
at March 30, 2013 04:23 by hejaaa


Updated Code
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;
		}
	}
}

Revision: 60421
at March 30, 2013 04:21 by hejaaa


Updated Code
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)));
						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;
			}
		}
		
		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;
		}
	}
}

Revision: 60420
at November 12, 2012 19:13 by hejaaa


Updated Code
// EXAMPLE /////////////////////////////////

const BACKGROUND_MUSIC:string = "sound.BackgroundMusic";
const OVER_BUTTON:string = "sound.OverButton";

SoundMix.play(BACKGROUND_MUSIC);


button1.addEventListener(MouseEvent.MOUSE_OVER, button1Over);
button1.addEventListener(MouseEvent.ROLL_OUT, button1Out);

private function button1Over(e:MouseEvent):void {
	SoundMix.play(OVER_BUTTON, false);
}

private function button1Out(e:MouseEvent):void {
	SoundMix.stop(OVER_BUTTON);
}

////////////////////////////////////////////

package com.sound {
	import flash.media.*;
	import flash.events.*;
	import flash.utils.getDefinitionByName;
	import flash.external.ExternalInterface;
	
	/**
	 * @author FCI
	 * @version 1.1
	 */
	public class SoundMix {
		private static var _mute:Boolean;
		protected static var playing:Boolean;
		protected static var dic:Object = { };
		
		/**
		 * umozni zapinat/vypinat zvuky cez JavaScript s definovanou funkciou v parametri functionName
		 * @param functionName nazov funkcie v JS
		 */
		public static function activate(functionName:String = "toggleMuteFlash"):void {
			if (ExternalInterface.available) {
				ExternalInterface.addCallback(functionName, toggleMute);
			}
		}
		
		protected static function toggleMute() {
			mute = !mute;	
		}	
		
		/**
		 * @param	soundID nazov CLASS s kompletnou cestou
		 */
		public static function stop(soundID:String):void {
			if (dic[soundID]) {
				dic[soundID].stop();
				dic[soundID].removeEventListener(Event.SOUND_COMPLETE, playComplete);
				dic[soundID] = null;
				playing = false;
			}
		}
		
		/**
		 * @param	soundID		nazov CLASS s kompletnou cestou
		 * @param	parallel	prehra aj pocas spustenej skladby
		 * @param	volume		hlasitost
		 * @param	loops		pocet opakovani
		 */
		public static function play(soundID:String, parallel:Boolean = true, volume:Number = 1, loops:int = 0):void {
			if ((parallel) || (!parallel && !playing)) {
				var soundClass:Class;
				var sound:Sound;
				var channel:SoundChannel;
				soundClass = getDefinitionByName(soundID) as Class;
				sound = new soundClass();
				channel = sound.play(0, loops, new SoundTransform(volume));
				dic[soundID] = channel;
				if (!parallel) {
					channel.addEventListener(Event.SOUND_COMPLETE, playComplete);
					playing = true;
				}
			}
		}
		
		/**
		 * overi ci sa prave prehrava skladba s konkretnym soundID
		 * @param	soundID nazov CLASS s kompletnou cestou
		 * @return	navratova hodnota vyhodnocujuca prehravanie
		 */
		//TODO: nedokonale overovanie prehravania
		public static function isPlaying(soundID:String):Boolean {
			var playing:Boolean;
			if (dic[soundID] && dic[soundID].position != 0) {
				playing = true;
			}else {
				playing = false;
			}
			
			return playing;
		}
		
		protected static function playComplete(e:Event):void {
			playing = false;
		}
		
		static public function get mute():Boolean {
			return _mute;
		}
		
		static public function set mute(value:Boolean):void {
			_mute = value;
			
			var st:SoundTransform = new SoundTransform();
			st.volume = _mute ? 0 : 1;
			SoundMixer.soundTransform = st;	
		}
	}
}

Revision: 60419
at November 12, 2012 19:12 by hejaaa


Updated Code
// EXAMPLE /////////////////////////////////

const BACKGROUND_MUSIC:string = "sound.BackgroundMusic";
const OVER_BUTTON:string = "sound.OverButton";

SoundFX.play(BACKGROUND_MUSIC);


button1.addEventListener(MouseEvent.MOUSE_OVER, button1Over);
button1.addEventListener(MouseEvent.ROLL_OUT, button1Out);

private function button1Over(e:MouseEvent):void {
	SoundFX.play(OVER_BUTTON, false);
}

private function button1Out(e:MouseEvent):void {
	SoundFX.stop(OVER_BUTTON);
}

////////////////////////////////////////////

package com.sound {
	import flash.media.*;
	import flash.events.*;
	import flash.utils.getDefinitionByName;
	import flash.external.ExternalInterface;
	
	/**
	 * @author FCI
	 * @version 1.1
	 */
	public class SoundMix {
		private static var _mute:Boolean;
		protected static var playing:Boolean;
		protected static var dic:Object = { };
		
		/**
		 * umozni zapinat/vypinat zvuky cez JavaScript s definovanou funkciou v parametri functionName
		 * @param functionName nazov funkcie v JS
		 */
		public static function activate(functionName:String = "toggleMuteFlash"):void {
			if (ExternalInterface.available) {
				ExternalInterface.addCallback(functionName, toggleMute);
			}
		}
		
		protected static function toggleMute() {
			mute = !mute;	
		}	
		
		/**
		 * @param	soundID nazov CLASS s kompletnou cestou
		 */
		public static function stop(soundID:String):void {
			if (dic[soundID]) {
				dic[soundID].stop();
				dic[soundID].removeEventListener(Event.SOUND_COMPLETE, playComplete);
				dic[soundID] = null;
				playing = false;
			}
		}
		
		/**
		 * @param	soundID		nazov CLASS s kompletnou cestou
		 * @param	parallel	prehra aj pocas spustenej skladby
		 * @param	volume		hlasitost
		 * @param	loops		pocet opakovani
		 */
		public static function play(soundID:String, parallel:Boolean = true, volume:Number = 1, loops:int = 0):void {
			if ((parallel) || (!parallel && !playing)) {
				var soundClass:Class;
				var sound:Sound;
				var channel:SoundChannel;
				soundClass = getDefinitionByName(soundID) as Class;
				sound = new soundClass();
				channel = sound.play(0, loops, new SoundTransform(volume));
				dic[soundID] = channel;
				if (!parallel) {
					channel.addEventListener(Event.SOUND_COMPLETE, playComplete);
					playing = true;
				}
			}
		}
		
		/**
		 * overi ci sa prave prehrava skladba s konkretnym soundID
		 * @param	soundID nazov CLASS s kompletnou cestou
		 * @return	navratova hodnota vyhodnocujuca prehravanie
		 */
		//TODO: nedokonale overovanie prehravania
		public static function isPlaying(soundID:String):Boolean {
			var playing:Boolean;
			if (dic[soundID] && dic[soundID].position != 0) {
				playing = true;
			}else {
				playing = false;
			}
			
			return playing;
		}
		
		protected static function playComplete(e:Event):void {
			playing = false;
		}
		
		static public function get mute():Boolean {
			return _mute;
		}
		
		static public function set mute(value:Boolean):void {
			_mute = value;
			
			var st:SoundTransform = new SoundTransform();
			st.volume = _mute ? 0 : 1;
			SoundMixer.soundTransform = st;	
		}
	}
}

Revision: 60418
at November 7, 2012 20:07 by hejaaa


Updated Code
// EXAMPLE /////////////////////////////////

const BACKGROUND_MUSIC:string = "sound.BackgroundMusic";
const OVER_BUTTON:string = "sound.OverButton";

SoundFX.play(BACKGROUND_MUSIC);


button1.addEventListener(MouseEvent.MOUSE_OVER, button1Over);
button1.addEventListener(MouseEvent.ROLL_OUT, button1Out);

private function button1Over(e:MouseEvent):void {
	SoundFX.play(OVER_BUTTON, false);
}

private function button1Out(e:MouseEvent):void {
	SoundFX.stop(OVER_BUTTON);
}

////////////////////////////////////////////

package com.sound{
	import flash.media.*;
	import flash.events.*;
	import flash.utils.getDefinitionByName;
	
	/**
	 * @author FCI
	 */
	public class SoundFX {
		protected static var playing:Boolean;
		protected static var dic:Object = {};
		
		/**
		 * @param	soundID nazov CLASS s kompletnou cestou
		 */
		public static function stop(soundID:String):void {
			if(dic[soundID]){
				dic[soundID].stop();
				dic[soundID].removeEventListener(Event.SOUND_COMPLETE, playComplete);
				dic[soundID] = null;
				playing = false;
			}
		}
		
		/**
		 * @param	soundID		nazov CLASS s kompletnou cestou
		 * @param	parallel	prehra aj pocas spustenej skladby
		 * @param	volume		hlasitost
		 * @param	loops		pocet opakovani
		 */
		public static function play(soundID:String, parallel:Boolean = true, volume:Number = 1, loops:int = 0):void {
			if ((parallel) || (!parallel && !playing)) {
				var soundClass:Class;
				var sound:Sound;
				var channel:SoundChannel;
				soundClass = getDefinitionByName(soundID) as Class;
				sound = new soundClass();
				channel = sound.play(0, loops, new SoundTransform(volume));
				dic[soundID] = channel;
				if(!parallel){
					channel.addEventListener(Event.SOUND_COMPLETE, playComplete);
					playing = true;
				}
			}
		}
		
		protected static function playComplete(e:Event):void {
			playing = false;
		}
	}
}

Revision: 60417
at November 7, 2012 20:06 by hejaaa


Initial Code
// EXAMPLE /////////////////////////////////

const BACKGROUND_MUSIC:string = "sound.BackgroundMusic";
const OVER_BUTTON:string = "sound.OverButton";
const OUT_BUTTON:string = "sound.OutButton";

SoundFX.play(BACKGROUND_MUSIC);


button1.addEventListener(MouseEvent.MOUSE_OVER, button1Over);
button1.addEventListener(MouseEvent.ROLL_OUT, button1Out);

private function button1Over(e:MouseEvent):void {
	SoundFX.play(OVER_BUTTON, false);
}

private function button1Out(e:MouseEvent):void {
	SoundFX.stop(OUT_BUTTON);
}

////////////////////////////////////////////

package com.sound{
	import flash.media.*;
	import flash.events.*;
	import flash.utils.getDefinitionByName;
	
	/**
	 * @author FCI
	 */
	public class SoundFX {
		protected static var playing:Boolean;
		protected static var dic:Object = {};
		
		/**
		 * @param	soundID nazov CLASS s kompletnou cestou
		 */
		public static function stop(soundID:String):void {
			if(dic[soundID]){
				dic[soundID].stop();
				dic[soundID].removeEventListener(Event.SOUND_COMPLETE, playComplete);
				dic[soundID] = null;
				playing = false;
			}
		}
		
		/**
		 * @param	soundID		nazov CLASS s kompletnou cestou
		 * @param	parallel	prehra aj pocas spustenej skladby
		 * @param	volume		hlasitost
		 * @param	loops		pocet opakovani
		 */
		public static function play(soundID:String, parallel:Boolean = true, volume:Number = 1, loops:int = 0):void {
			if ((parallel) || (!parallel && !playing)) {
				var soundClass:Class;
				var sound:Sound;
				var channel:SoundChannel;
				soundClass = getDefinitionByName(soundID) as Class;
				sound = new soundClass();
				channel = sound.play(0, loops, new SoundTransform(volume));
				dic[soundID] = channel;
				if(!parallel){
					channel.addEventListener(Event.SOUND_COMPLETE, playComplete);
					playing = true;
				}
			}
		}
		
		protected static function playComplete(e:Event):void {
			playing = false;
		}
	}
}

Initial URL

                                

Initial Description
this Class can play sound from url or library in one parameter

Initial Title
universal class for playing sound

Initial Tags

                                

Initial Language
ActionScript 3