Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Gemfruit.Audio {
- import flash.events.Event;
- import flash.media.Sound;
- import flash.media.SoundChannel;
- import flash.media.SoundTransform;
- import ShadowLib.Core.GameObject;
- import ShadowLib.Audio.GameSound;
- import ShadowLib.Input.Keyboard;
- import ShadowLib.Input.Key;
- public class AudioManager {
- private var sounds:Vector.<GameSound>;
- private var soundNames:Vector.<String>;
- //Music control
- private var currentLoop:GameSound;
- //Muting
- private var soundOff:Boolean = false;
- private var musicOff:Boolean = false;
- //Reading Audio
- private var audioReader:AudioReader;
- //Singleton
- private static var instance:AudioManager;
- private static var allowInstantiation:Boolean;
- public function AudioManager() {
- if(allowInstantiation) {
- init();
- } else {
- throw new Error("Instantiation failed - Use getInstance() instead of new MapHandler()");
- }
- }
- public function init():void {
- sounds = new Vector.<GameSound>();
- soundNames = new Vector.<String>();
- }
- public static function getInstance():AudioManager {
- if (instance == null) {
- allowInstantiation = true;
- instance = new AudioManager();
- allowInstantiation = false;
- }
- return instance;
- }
- public function addSound(soundClass:Class, soundName:String):void {
- var sound:Sound = new soundClass();
- var gameSound:GameSound = new GameSound();
- gameSound.createFromSound(sound);
- sounds.push(gameSound);
- soundNames.push(soundName);
- }
- public function getSoundByID(soundID:uint):GameSound {
- return sounds[soundID];
- }
- public function getSoundByName(soundName:String):GameSound {
- var i:int = soundNames.indexOf(soundName);
- if(i > -1) {
- return sounds[i];
- }
- trace("Unable to find sound:", soundName);
- return null;
- }
- public function initAudioReader():void {
- audioReader = new AudioReader();
- audioReader.init();
- }
- public function changeLoop(soundName:String):void {
- if(currentLoop != null) currentLoop.stop();
- var newLoop:GameSound;
- newLoop = getSoundByName(soundName);
- currentLoop = newLoop;
- currentLoop.play(0, 999999);
- }
- public function getCurrentLoop():GameSound {
- return currentLoop;
- }
- public function stopLoop():void {
- if(currentLoop != null){
- currentLoop.stop();
- currentLoop = null;
- }
- }
- public function playSound(soundName:String):SoundChannel {
- return getSoundByName(soundName).play();
- }
- public function toggleMute():void {
- toggleSound();
- toggleMusic();
- }
- public function toggleSound():void {
- soundOff = (soundOff) ? false : true;
- }
- public function toggleMusic():void {
- musicOff = (musicOff) ? false : true;
- }
- public function update():void {
- if(Keyboard.isKeyDown(Key.M)) {
- toggleMute();
- }
- if(currentLoop != null) {
- if(musicOff) {
- if(!currentLoop.muted){
- currentLoop.mute();
- }
- }
- if(!musicOff) {
- if(currentLoop.muted) {
- currentLoop.play(currentLoop.savedPosition, 99999);
- }
- }
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment