Advertisement
Guest User

SoundManager.java

a guest
Jul 15th, 2014
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.70 KB | None | 0 0
  1. package tests.sound;
  2.  
  3. import tests.sound.LRUCache.CacheEntryRemovedListener;
  4. import tests.sound.SoundManager.STSSound;
  5.  
  6. import com.badlogic.gdx.Gdx;
  7. import com.badlogic.gdx.audio.Sound;
  8. import com.badlogic.gdx.files.FileHandle;
  9. import com.badlogic.gdx.utils.Disposable;
  10. import com.olo.spam_the_spacebar.GameClass;
  11.  
  12. /**
  13.  * A service that manages the sound effects.
  14.  */
  15. public class SoundManager implements CacheEntryRemovedListener<STSSound,Sound>,Disposable{
  16.    
  17.    
  18.     public enum STSSound{
  19.         NULL( "" ),//DO NOT REMOVE, PLAYS NOTHING
  20.             COINS( "audio/sfx/coins.wav" ),
  21.             BOOTLE_OF_WATER( "audio/sfx/bottleofwater.wav" ),
  22.             VOICE_SO_EASY( "audio/sfx/gameover_so_easy.wav" ),
  23.             SWOOSH( "audio/sfx/swoosh.wav" );
  24.  
  25.         private final String fileName;
  26.  
  27.         private STSSound(String fileName){
  28.             this.fileName = fileName;
  29.         }
  30.        
  31.         public STSSound getSound(String path){
  32.             for(STSSound sound : STSSound.values()){
  33.                 if(sound.getFileName().equals(path))
  34.                     return sound;
  35.             }
  36.             return NULL;
  37.         }
  38.  
  39.         public String getFileName()
  40.         {
  41.             return fileName;
  42.         }
  43.     }
  44.    
  45.     private float volume = 1f;
  46.    
  47.     private boolean enabled = true;//Whether the sound is enabled.
  48.  
  49.     private final LRUCache<STSSound,Sound> soundCache;//The sound cache.
  50.    
  51.     public static SoundManager instance;
  52.     static{
  53.         if(instance == null) new SoundManager();
  54.     }
  55.  
  56.     public SoundManager(){//Creates the sound manager.
  57.         if(instance != null){
  58.             volume = instance.volume;
  59.             enabled = instance.enabled;
  60.             soundCache = instance.soundCache;
  61.             instance.setEnabled(false);
  62.             instance = null;
  63.         }else{
  64.             soundCache = new LRUCache<SoundManager.STSSound,Sound>(10);
  65.             soundCache.setEntryRemovedListener( this );
  66.         }
  67.            
  68.         instance = this;
  69.     }
  70.  
  71.     public Sound play(STSSound sound){//Plays the specified sound.
  72.         if(sound == STSSound.NULL) return null;
  73.         // check if the sound is enabled
  74.         if( ! enabled ) return null;
  75.  
  76.         // try and get the sound from the cache
  77.         Sound soundToPlay = soundCache.get( sound );
  78.         if( soundToPlay == null ) {
  79.             FileHandle soundFile = Gdx.files.internal( sound.getFileName() );
  80.             soundToPlay = Gdx.audio.newSound( soundFile );
  81.             soundCache.add( sound, soundToPlay );
  82.         }
  83.  
  84.         // play the sound
  85.         //Gdx.app.log( TheGoldenTemple.LOG, "Playing sound: " + sound.name() );
  86.         soundToPlay.play( volume );
  87.         return soundToPlay;
  88.     }
  89.  
  90.     public void setVolume(float volume ){//Sets the sound volume which must be inside the range [0,1].
  91.         Gdx.app.log( GameClass.LOG, "Adjusting sound volume to: " + volume );
  92.  
  93.         // check and set the new volume
  94.         if( volume < 0 || volume > 1f ) {
  95.             throw new IllegalArgumentException( "The volume must be inside the range: [0,1]" );
  96.         }
  97.         this.volume = volume;
  98.     }
  99.  
  100.     public void setEnabled(boolean enabled){//Enables or disabled the sound.
  101.        
  102.         this.enabled = enabled;
  103.     }
  104.    
  105.     @Override
  106.     public void notifyEntryRemoved(STSSound key,Sound value){
  107.        
  108.         Gdx.app.log( GameClass.LOG, "Disposing sound: " + key.name() );
  109.         value.dispose();
  110.     }
  111.  
  112.     public void dispose(){//Disposes the sound manager.
  113.         Gdx.app.log( GameClass.LOG, "Disposing sound manager" );
  114.         for( Sound sound : soundCache.retrieveAll() ) {
  115.             sound.stop();
  116.             sound.dispose();
  117.         }
  118.     }
  119.    
  120.     public float getVolume(){
  121.         return volume;
  122.     }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement