Guest User

Untitled

a guest
Jun 17th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package Gemfruit.Audio {
  2.    
  3.     import flash.events.Event;
  4.     import flash.media.Sound;
  5.     import flash.media.SoundChannel;
  6.     import flash.media.SoundTransform;
  7.     import ShadowLib.Core.GameObject;
  8.     import ShadowLib.Audio.GameSound;
  9.     import ShadowLib.Input.Keyboard;
  10.     import ShadowLib.Input.Key;
  11.    
  12.     public class AudioManager {
  13.        
  14.         private var sounds:Vector.<GameSound>;
  15.         private var soundNames:Vector.<String>;
  16.        
  17.         //Music control
  18.         private var currentLoop:GameSound;
  19.         //Muting
  20.         private var soundOff:Boolean = false;
  21.         private var musicOff:Boolean = false;
  22.         //Reading Audio
  23.         private var audioReader:AudioReader;
  24.         //Singleton
  25.         private static var instance:AudioManager;
  26.         private static var allowInstantiation:Boolean;
  27.        
  28.         public function AudioManager() {
  29.             if(allowInstantiation) {
  30.                 init();
  31.             } else {
  32.                 throw new Error("Instantiation failed - Use getInstance() instead of new MapHandler()");
  33.             }
  34.         }
  35.        
  36.         public function init():void {
  37.             sounds = new Vector.<GameSound>();
  38.             soundNames = new Vector.<String>();
  39.         }
  40.        
  41.         public static function getInstance():AudioManager {
  42.             if (instance == null) {
  43.                 allowInstantiation = true;
  44.                 instance = new AudioManager();
  45.                 allowInstantiation = false;
  46.             }
  47.             return instance;
  48.         }
  49.        
  50.         public function addSound(soundClass:Class, soundName:String):void {
  51.             var sound:Sound = new soundClass();
  52.             var gameSound:GameSound = new GameSound();
  53.             gameSound.createFromSound(sound);
  54.             sounds.push(gameSound);
  55.             soundNames.push(soundName);
  56.         }
  57.        
  58.         public function getSoundByID(soundID:uint):GameSound {
  59.             return sounds[soundID];
  60.         }
  61.        
  62.         public function getSoundByName(soundName:String):GameSound {
  63.             var i:int = soundNames.indexOf(soundName);
  64.             if(i > -1) {
  65.                 return sounds[i];
  66.             }
  67.             trace("Unable to find sound:", soundName);
  68.             return null;
  69.         }
  70.        
  71.         public function initAudioReader():void {
  72.             audioReader = new AudioReader();
  73.             audioReader.init();
  74.         }
  75.        
  76.         public function changeLoop(soundName:String):void {
  77.             if(currentLoop != null) currentLoop.stop();
  78.            
  79.             var newLoop:GameSound;
  80.             newLoop = getSoundByName(soundName);
  81.            
  82.             currentLoop = newLoop;
  83.             currentLoop.play(0, 999999);
  84.         }
  85.        
  86.         public function getCurrentLoop():GameSound {
  87.             return currentLoop;
  88.         }
  89.        
  90.         public function stopLoop():void {
  91.             if(currentLoop != null){
  92.                 currentLoop.stop();
  93.                 currentLoop = null;
  94.             }
  95.         }
  96.        
  97.         public function playSound(soundName:String):SoundChannel {
  98.             return getSoundByName(soundName).play();
  99.         }
  100.        
  101.         public function toggleMute():void {
  102.             toggleSound();
  103.             toggleMusic();
  104.         }
  105.        
  106.         public function toggleSound():void {
  107.             soundOff = (soundOff) ? false : true;
  108.         }
  109.        
  110.         public function toggleMusic():void {
  111.             musicOff = (musicOff) ? false : true;
  112.         }
  113.        
  114.         public function update():void {
  115.             if(Keyboard.isKeyDown(Key.M)) {
  116.                 toggleMute();
  117.             }
  118.             if(currentLoop != null) {
  119.                 if(musicOff) {
  120.                     if(!currentLoop.muted){
  121.                         currentLoop.mute();
  122.                     }
  123.                 }
  124.                 if(!musicOff) {
  125.                     if(currentLoop.muted) {
  126.                         currentLoop.play(currentLoop.savedPosition, 99999);
  127.                     }
  128.                 }
  129.             }
  130.         }
  131.        
  132.     }
  133.    
  134. }
Add Comment
Please, Sign In to add comment