Advertisement
Guest User

MusicManager.java

a guest
Jul 15th, 2014
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.83 KB | None | 0 0
  1. package tests.sound;
  2.  
  3. import java.util.ArrayList;
  4.  
  5. import com.badlogic.gdx.Gdx;
  6. import com.badlogic.gdx.audio.Music;
  7. import com.badlogic.gdx.files.FileHandle;
  8. import com.badlogic.gdx.utils.Disposable;
  9. import com.olo.spam_the_spacebar.GameClass;
  10.  
  11. /**
  12.  * A service that manages the background music.
  13.  * <p>
  14.  * Only one music may be playing at a given time.
  15.  */
  16. public class MusicManager implements Disposable {
  17.    
  18.     //available music files.
  19.     public enum STSMusic{
  20.         CAMPFIRE( "audio/music/campfire.ogg" ),
  21.         HARDWIND( "audio/music/hardwind.ogg" ),
  22.         RAIN_HARDWIND( "audio/music/rain&hardwind.ogg" ),
  23.         TREES_WEAVING( "audio/music/trees_weaving.ogg" );
  24.  
  25.         private final String fileName;
  26.  
  27.         private STSMusic(String fileName){
  28.             this.fileName = fileName;
  29.         }
  30.  
  31.         public String getFileName(){
  32.             return fileName;
  33.         }
  34.     }
  35.  
  36.     private ArrayList<Music> musicsBeingPlayed;
  37.     private float volume = 1f;
  38.     private boolean enabled = false;
  39.  
  40.     public static MusicManager instance;
  41.     static{
  42.         if(instance == null) new MusicManager();
  43.     }
  44.    
  45.     public MusicManager(){
  46.          if(instance != null){
  47.             volume = instance.volume;
  48.             enabled = instance.enabled;
  49.             if(instance.isAnyMusicPlaying())
  50.                 instance.stop();
  51.             else
  52.                 musicsBeingPlayed = instance.musicsBeingPlayed;
  53.             instance.setEnabled(false);
  54.             instance = null;
  55.          }else{
  56.              musicsBeingPlayed = new ArrayList<Music>();
  57.          }
  58.            
  59.          instance = this;
  60.     }
  61.  
  62.     //Plays the given music (starts the streaming).
  63.     //If there is already a music being played it is stopped automatically.
  64.     public void play(STSMusic music){
  65.         if( ! enabled ) return;
  66.  
  67.         // stop any music being played
  68.         Gdx.app.log( GameClass.LOG, "Playing music: " + music.name() );
  69.  
  70.         // start streaming the new music
  71.         FileHandle musicFile = Gdx.files.internal( music.getFileName() );
  72.         Music musicBeingPlayed = Gdx.audio.newMusic( musicFile );
  73.         musicBeingPlayed.setVolume( volume );
  74.         musicBeingPlayed.setLooping( true );
  75.         musicBeingPlayed.play();
  76.         musicsBeingPlayed.add(musicBeingPlayed);
  77.     }
  78.    
  79.     public boolean isAnyMusicPlaying(){
  80.         for(Music music : musicsBeingPlayed)
  81.             if(music.isPlaying())
  82.                 return true;
  83.         return false;
  84.     }
  85.  
  86.     //Stops and disposes the current music being played, if any.
  87.     public void stop(){
  88.         if(isAnyMusicPlaying()){
  89.             Gdx.app.log( GameClass.LOG, "Stopping current music" );
  90.             for(Music music : musicsBeingPlayed){
  91.                 music.stop();
  92.                 music.dispose();
  93.             }
  94.             musicsBeingPlayed.clear();
  95.         }
  96.     }
  97.  
  98.     //Sets the music volume which must be inside the range [0,1].
  99.     public void setVolume(float volume){
  100.         Gdx.app.log( GameClass.LOG, "Adjusting music volume to: " + volume );
  101.  
  102.         // check and set the new volume
  103.         if( volume < 0 || volume > 1f ) {
  104.             throw new IllegalArgumentException( "The volume must be inside the range: [0,1]" );
  105.         }
  106.         this.volume = volume;
  107.  
  108.         // if there is a music being played, change its volume
  109.         if(isAnyMusicPlaying()) {
  110.            for(Music music : musicsBeingPlayed){
  111.                music.setVolume(volume);
  112.            }
  113.         }
  114.     }
  115.  
  116.     public void setEnabled(boolean enabled){
  117.         this.enabled = enabled;
  118.  
  119.         // if the music is being deactivated, stop any music being played
  120.         if(!enabled){
  121.             stop();
  122.         }
  123.     }
  124.    
  125.     public void dispose(){
  126.         Gdx.app.log( GameClass.LOG, "Disposing music manager" );
  127.         stop();
  128.     }
  129.    
  130.     public float getVolume(){
  131.         return volume;
  132.     }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement