Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package tests.sound;
- import tests.sound.LRUCache.CacheEntryRemovedListener;
- import tests.sound.SoundManager.STSSound;
- import com.badlogic.gdx.Gdx;
- import com.badlogic.gdx.audio.Sound;
- import com.badlogic.gdx.files.FileHandle;
- import com.badlogic.gdx.utils.Disposable;
- import com.olo.spam_the_spacebar.GameClass;
- /**
- * A service that manages the sound effects.
- */
- public class SoundManager implements CacheEntryRemovedListener<STSSound,Sound>,Disposable{
- public enum STSSound{
- NULL( "" ),//DO NOT REMOVE, PLAYS NOTHING
- COINS( "audio/sfx/coins.wav" ),
- BOOTLE_OF_WATER( "audio/sfx/bottleofwater.wav" ),
- VOICE_SO_EASY( "audio/sfx/gameover_so_easy.wav" ),
- SWOOSH( "audio/sfx/swoosh.wav" );
- private final String fileName;
- private STSSound(String fileName){
- this.fileName = fileName;
- }
- public STSSound getSound(String path){
- for(STSSound sound : STSSound.values()){
- if(sound.getFileName().equals(path))
- return sound;
- }
- return NULL;
- }
- public String getFileName()
- {
- return fileName;
- }
- }
- private float volume = 1f;
- private boolean enabled = true;//Whether the sound is enabled.
- private final LRUCache<STSSound,Sound> soundCache;//The sound cache.
- public static SoundManager instance;
- static{
- if(instance == null) new SoundManager();
- }
- public SoundManager(){//Creates the sound manager.
- if(instance != null){
- volume = instance.volume;
- enabled = instance.enabled;
- soundCache = instance.soundCache;
- instance.setEnabled(false);
- instance = null;
- }else{
- soundCache = new LRUCache<SoundManager.STSSound,Sound>(10);
- soundCache.setEntryRemovedListener( this );
- }
- instance = this;
- }
- public Sound play(STSSound sound){//Plays the specified sound.
- if(sound == STSSound.NULL) return null;
- // check if the sound is enabled
- if( ! enabled ) return null;
- // try and get the sound from the cache
- Sound soundToPlay = soundCache.get( sound );
- if( soundToPlay == null ) {
- FileHandle soundFile = Gdx.files.internal( sound.getFileName() );
- soundToPlay = Gdx.audio.newSound( soundFile );
- soundCache.add( sound, soundToPlay );
- }
- // play the sound
- //Gdx.app.log( TheGoldenTemple.LOG, "Playing sound: " + sound.name() );
- soundToPlay.play( volume );
- return soundToPlay;
- }
- public void setVolume(float volume ){//Sets the sound volume which must be inside the range [0,1].
- Gdx.app.log( GameClass.LOG, "Adjusting sound volume to: " + volume );
- // check and set the new volume
- if( volume < 0 || volume > 1f ) {
- throw new IllegalArgumentException( "The volume must be inside the range: [0,1]" );
- }
- this.volume = volume;
- }
- public void setEnabled(boolean enabled){//Enables or disabled the sound.
- this.enabled = enabled;
- }
- @Override
- public void notifyEntryRemoved(STSSound key,Sound value){
- Gdx.app.log( GameClass.LOG, "Disposing sound: " + key.name() );
- value.dispose();
- }
- public void dispose(){//Disposes the sound manager.
- Gdx.app.log( GameClass.LOG, "Disposing sound manager" );
- for( Sound sound : soundCache.retrieveAll() ) {
- sound.stop();
- sound.dispose();
- }
- }
- public float getVolume(){
- return volume;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement