package { /* * Author: Andy Moore ( http://www.andymoore.ca ) * Updated: October 2011 */ import flash.display.DisplayObject; import flash.display.MovieClip; import flash.display.Sprite; import flash.display.Stage; import flash.media.Sound; import flash.media.SoundChannel; import flash.media.SoundLoaderContext; import flash.media.SoundTransform; import flash.net.URLRequest; import flash.events.*; import flash.utils.Dictionary; import org.flashdevelop.utils.FlashConnect; public class RadialSound { private static var instance:RadialSound; public var musicSoundChannel:SoundChannel = new SoundChannel(); // Only a single channel for music public var soundChannelSize:Number = 10; // How many sound channels public var effectsSoundChannel:Array = new Array(); // Array to store the sound channels public var soundLibrary:Dictionary = new Dictionary(); // Library of all stored sounds public var muted:Boolean; // Whether the game is muted or not, defined on creation public var onUnMutePlay:String; // What to play when unmuting (mute kills all streams) public var muteButtonArt:MovieClip; public var unMuteButtonArt:MovieClip; // To initialize, simply: // new RadialSound(); // // Then start storing your sound: // RadialSound.storeAudio("referenceName", audioClass); public function RadialSound():void { if (instance) { throw new Error("Sound instance already exists."); } instance = this; muted = Config.startMuted; } /** * * @param referenceName Name to refer to this audio asset in the future * @param audioClass If this is a String, will stream it in as-needed. If Sound class, will load. * @param startStreamNow If true, will immediately begin streaming the audio for storage (if audioClass is string) * @param setAsUnMuteMusic Upon un-muting, this audio will resume playing. */ public static function storeAudio(referenceName:String, audioClass:*, startStreamNow:Boolean = true, setAsUnMuteMusic:Boolean=false):void { if (audioClass is String) { instance.soundLibrary[referenceName] = audioClass as String; if (startStreamNow) { instance.startStream(referenceName); } else { } } else { instance.soundLibrary[referenceName] = audioClass; } if (setAsUnMuteMusic) instance.onUnMutePlay = referenceName; } public function startStream(which:String):void { var toStream:Sound = new Sound(); var theURL:String = soundLibrary[which]; if (soundLibrary[which] is String) { var req:URLRequest = new URLRequest(theURL); var context:SoundLoaderContext = new SoundLoaderContext(); toStream.load(req, context); soundLibrary[which] = toStream; } else { throw new Error("Can only stream assets that are URLs, "+which+" is an audio class"); } } public static function playMusic(assetName:String, loop:Boolean = true):void { if (instance.soundLibrary[assetName]) { if (instance.soundLibrary[assetName] is String) { instance.startStream(assetName); } var loops:Number = 0; if (loop) loops = 9999; var sound:Sound = new instance.soundLibrary[assetName]; instance.musicSoundChannel = sound.play(0, loops); } else { throw new Error("No such asset: "+assetName); } } public static function playSound(which:String):void { if (instance.muted) return; while (instance.effectsSoundChannel.length > instance.soundChannelSize) { instance.effectsSoundChannel = instance.effectsSoundChannel.slice(1); } var newSound:Sound = new instance.soundLibrary[which]; // TODO Can probably set volume right in the play command - look into instance.effectsSoundChannel.push(newSound.play(0, 1)); } public function setMute(mute:Boolean):void { if (mute) { for (var i:Number = 0; i < effectsSoundChannel.length; i++) { if (effectsSoundChannel[i]) effectsSoundChannel[i].stop(); } if (musicSoundChannel) musicSoundChannel.stop(); } else { if (onUnMutePlay) playMusic(onUnMutePlay); } muted = mute; } public static function createMuteButton(stageObject:Stage, muteArt:MovieClip, unMuteArt:MovieClip):void { instance.muteButtonArt = muteArt; instance.unMuteButtonArt = unMuteArt; instance.muteButtonArt.x = 5; instance.muteButtonArt.y = DisplayProperties.screenY - 5 - instance.muteButtonArt.height; instance.unMuteButtonArt.x = instance.muteButtonArt.x; instance.unMuteButtonArt.y = instance.muteButtonArt.y; if (instance.muted) { instance.muteButtonArt.visible = false; instance.unMuteButtonArt.visible = true; } else { instance.muteButtonArt.visible = true; instance.unMuteButtonArt.visible = false; } stageObject.addChild(instance.unMuteButtonArt); stageObject.addChild(instance.muteButtonArt); instance.muteButtonArt.addEventListener(MouseEvent.CLICK, instance.muteClicked); instance.unMuteButtonArt.addEventListener(MouseEvent.CLICK, instance.unMuteClicked); } public function muteClicked(e:MouseEvent):void { muteButtonArt.visible = false; unMuteButtonArt.visible = true; setMute(true); } public function unMuteClicked(e:MouseEvent):void { unMuteButtonArt.visible = false; muteButtonArt.visible = true; setMute(false); } } }