Advertisement
Guest User

AS3 SoundManager Class

a guest
Feb 8th, 2013
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package
  2. {
  3.     import flash.events.Event;
  4.     import flash.media.Sound;
  5.     import flash.media.SoundChannel;
  6.     import flash.media.SoundTransform;
  7.    
  8.     /**
  9.      * ...
  10.      * @author RTLShadow
  11.      */
  12.     public class SoundManager
  13.     {
  14.         private static var soundNames:Array = [];
  15.         private static var soundList:Array = [];
  16.        
  17.         private static var _soundMuted:Boolean = false;
  18.         private static var _musicMuted:Boolean = false;
  19.         private static var _soundVol:Number = 1;
  20.         private static var _musicVol:Number = 1;
  21.        
  22.         public static const SOUND:int = 0;
  23.         public static const MUSIC:int = 1;
  24.         public static const OTHER:int = 2;
  25.        
  26.         /**
  27.          * Adds a sound to the list of sounds that you can play.
  28.          * @param   s   The Sound object to add to the list.
  29.          * @param   name    The name of the sound, you cannot have duplicates in the same list.
  30.          * @param   type    The type to classify the sound under. Defaults are SOUND(0), MUSIC(1), and OTHER(2). Used to classify what sounds to mute.
  31.          */
  32.         public static function addSound( s:Sound, name:String, type:int ):void
  33.         {
  34.             if ( soundList[ name ] == undefined )
  35.             {
  36.                 var st:SoundTransform = new SoundTransform(( type == SOUND ) ? _soundVol : (( type == MUSIC ) ? _musicVol : 1 ) );
  37.                 soundList[ name ] = { sound: s, // sound object
  38.                         transform: st, // SoundTransform
  39.                         channel: s.play(), // SoundChannel
  40.                         type: type, // the type of sound (i.e. SOUND, MUSIC, OTHER)
  41.                         isPlaying: false }; // if the sound is currently playing
  42.                
  43.                 soundNames.push( name );
  44.                 soundList[ name ].channel.stop();
  45.             }
  46.             else
  47.             {
  48.                 throw( new Error( "The name '" + name + "' already exists for a sound name." ) )
  49.             }
  50.         }
  51.        
  52.         /**
  53.          * Play a sound from the current list. The sound must have been added through the 'addSound()' function before playing it.
  54.          * @param   name        The name of the sound to play.
  55.          * @param   loops   The amount to loop the sound, the default is 0. -1 to repeat infinitely.
  56.          */
  57.         public static function playSound( name:String, loops:int = 0 ):void
  58.         {
  59.             if ( soundList[ name ] != undefined )
  60.             {
  61.                 soundList[ name ].channel = soundList[ name ].sound.play( 0, // position
  62.                     ( loops == -1 ) ? int.MAX_VALUE : loops, // loop amount
  63.                     soundList[ name ].transform ); // transform to apply
  64.                
  65.                 soundList[ name ].isPlaying = true;
  66.                 soundList[ name ].channel.addEventListener( Event.SOUND_COMPLETE, soundComplete );
  67.             }
  68.             else
  69.             {
  70.                 throw( new Error( "Sound '" + name + "' does not exist." ) );
  71.             }
  72.         }
  73.        
  74.         private static function soundComplete( e:Event ):void
  75.         {
  76.             // TODO Find a better way to do this entire function
  77.             for ( var i:int = soundList.length - 1; i >= 0; --i )
  78.             {
  79.                 if ( soundList[ i ].channel == e.target )
  80.                 {
  81.                     soundList[ i ].isPlaying = false;
  82.                     break;
  83.                 }
  84.             }
  85.         }
  86.        
  87.         /**
  88.          * Stop the sound being played.
  89.          * @param   name    The name of the sound to stop.
  90.          */
  91.         public static function stopSound( name:String ):void
  92.         {
  93.             if ( soundList[ name ].sound != undefined )
  94.             {
  95.                 soundList[ name ].channel.stop();
  96.             }
  97.             else
  98.             {
  99.                 throw( new Error( "Sound '" + name + "' does not exist." ) );
  100.             }
  101.         }
  102.        
  103.         public static function isPlaying( name:String ):Boolean
  104.         {
  105.             return soundList[ name ].isPlaying;
  106.         }
  107.        
  108.         /**
  109.          * Get the SoundTransform object of a specific sound.
  110.          * @param   name
  111.          * @return  The current SoundTransform of the sound.
  112.          */
  113.         public static function getTransform( name:String ):SoundTransform
  114.         {
  115.             return soundList[ name ].transform;
  116.         }
  117.        
  118.         /**
  119.          * Set the SoundTransform of a sound.
  120.          * @param   name            The name of the sound to set a new SoundTransform.
  121.          * @param   sTransform  SoundTransform object to set as the new soundTransform.
  122.          */
  123.         public static function setTransform( name:String, sTransform:SoundTransform ):void
  124.         {
  125.             soundList[ name ].transform = sTransform;
  126.             soundList[ name ].channel.soundTransform = sTransform;
  127.         }
  128.        
  129.         /**
  130.          * See if all sounds of type 'SOUND' are muted.
  131.          */
  132.         static public function get soundMuted():Boolean
  133.         {
  134.             return _soundMuted;
  135.         }
  136.        
  137.         /**
  138.          * Mute all sounds of type 'SOUND'.
  139.          */
  140.         static public function set soundMuted( m:Boolean ):void
  141.         {
  142.             _soundMuted = m;
  143.             var s:String = "";
  144.             var currTransform:SoundTransform;
  145.             for each ( s in soundNames )
  146.             {
  147.                 if ( soundList[ s ].type == SOUND )
  148.                 {
  149.                     currTransform = getTransform( s );
  150.                     if ( m )
  151.                         currTransform.volume = 0;
  152.                     else
  153.                         currTransform.volume = _soundVol;
  154.                     setTransform( s, currTransform );
  155.                 }
  156.             }
  157.         }
  158.        
  159.         /**
  160.          * See if all sounds of type 'MUSIC' are muted.
  161.          */
  162.         static public function get musicMuted():Boolean
  163.         {
  164.             return _musicMuted;
  165.         }
  166.        
  167.         /**
  168.          * Mute all sounds of type 'MUSIC'.
  169.          */
  170.         static public function set musicMuted( m:Boolean ):void
  171.         {
  172.             _musicMuted = m;
  173.             _soundMuted = m;
  174.             var s:String = "";
  175.             var currTransform:SoundTransform;
  176.             for each ( s in soundNames )
  177.             {
  178.                 if ( soundList[ s ].type == MUSIC )
  179.                 {
  180.                     currTransform = getTransform( s );
  181.                     if ( m )
  182.                         currTransform.volume = 0;
  183.                     else
  184.                         currTransform.volume = _musicVol;
  185.                     setTransform( s, currTransform );
  186.                 }
  187.             }
  188.         }
  189.        
  190.         static public function get soundVol():Number
  191.         {
  192.             return _soundVol;
  193.         }
  194.        
  195.         static public function set soundVol( s:Number ):void
  196.         {
  197.             _soundVol = s;
  198.             var str:String = "";
  199.             var currTransform:SoundTransform;
  200.             for each ( str in soundNames )
  201.             {
  202.                 if ( soundList[ str ].type == SOUND )
  203.                 {
  204.                     currTransform = getTransform( str );
  205.                     currTransform.volume = _soundVol;
  206.                     setTransform( str, currTransform );
  207.                 }
  208.             }
  209.         }
  210.        
  211.         static public function get musicVol():Number
  212.         {
  213.             return _musicVol;
  214.         }
  215.        
  216.         static public function set musicVol( m:Number ):void
  217.         {
  218.             _musicVol = m;
  219.             var str:String = "";
  220.             var currTransform:SoundTransform;
  221.             for each ( str in soundNames )
  222.             {
  223.                 if ( soundList[ str ].type == MUSIC )
  224.                 {
  225.                     currTransform = getTransform( str );
  226.                     currTransform.volume = _musicVol;
  227.                     setTransform( str, currTransform );
  228.                 }
  229.             }
  230.         }
  231.     }
  232.  
  233. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement