Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package tests.sound;
- import java.util.ArrayList;
- import com.badlogic.gdx.Gdx;
- import com.badlogic.gdx.audio.Music;
- import com.badlogic.gdx.files.FileHandle;
- import com.badlogic.gdx.utils.Disposable;
- import com.olo.spam_the_spacebar.GameClass;
- /**
- * A service that manages the background music.
- * <p>
- * Only one music may be playing at a given time.
- */
- public class MusicManager implements Disposable {
- //available music files.
- public enum STSMusic{
- CAMPFIRE( "audio/music/campfire.ogg" ),
- HARDWIND( "audio/music/hardwind.ogg" ),
- RAIN_HARDWIND( "audio/music/rain&hardwind.ogg" ),
- TREES_WEAVING( "audio/music/trees_weaving.ogg" );
- private final String fileName;
- private STSMusic(String fileName){
- this.fileName = fileName;
- }
- public String getFileName(){
- return fileName;
- }
- }
- private ArrayList<Music> musicsBeingPlayed;
- private float volume = 1f;
- private boolean enabled = false;
- public static MusicManager instance;
- static{
- if(instance == null) new MusicManager();
- }
- public MusicManager(){
- if(instance != null){
- volume = instance.volume;
- enabled = instance.enabled;
- if(instance.isAnyMusicPlaying())
- instance.stop();
- else
- musicsBeingPlayed = instance.musicsBeingPlayed;
- instance.setEnabled(false);
- instance = null;
- }else{
- musicsBeingPlayed = new ArrayList<Music>();
- }
- instance = this;
- }
- //Plays the given music (starts the streaming).
- //If there is already a music being played it is stopped automatically.
- public void play(STSMusic music){
- if( ! enabled ) return;
- // stop any music being played
- Gdx.app.log( GameClass.LOG, "Playing music: " + music.name() );
- // start streaming the new music
- FileHandle musicFile = Gdx.files.internal( music.getFileName() );
- Music musicBeingPlayed = Gdx.audio.newMusic( musicFile );
- musicBeingPlayed.setVolume( volume );
- musicBeingPlayed.setLooping( true );
- musicBeingPlayed.play();
- musicsBeingPlayed.add(musicBeingPlayed);
- }
- public boolean isAnyMusicPlaying(){
- for(Music music : musicsBeingPlayed)
- if(music.isPlaying())
- return true;
- return false;
- }
- //Stops and disposes the current music being played, if any.
- public void stop(){
- if(isAnyMusicPlaying()){
- Gdx.app.log( GameClass.LOG, "Stopping current music" );
- for(Music music : musicsBeingPlayed){
- music.stop();
- music.dispose();
- }
- musicsBeingPlayed.clear();
- }
- }
- //Sets the music volume which must be inside the range [0,1].
- public void setVolume(float volume){
- Gdx.app.log( GameClass.LOG, "Adjusting music 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;
- // if there is a music being played, change its volume
- if(isAnyMusicPlaying()) {
- for(Music music : musicsBeingPlayed){
- music.setVolume(volume);
- }
- }
- }
- public void setEnabled(boolean enabled){
- this.enabled = enabled;
- // if the music is being deactivated, stop any music being played
- if(!enabled){
- stop();
- }
- }
- public void dispose(){
- Gdx.app.log( GameClass.LOG, "Disposing music manager" );
- stop();
- }
- public float getVolume(){
- return volume;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement