Playing, changing and stopping music - UQcsse3200/2024-studio-1 GitHub Wiki

The LibGDX standard for playing music, AssetManager.getAsset(name, Music.class).play() won't account for the user-specified volume and mute settings from UserSettings. To fix this, the ResourceService has been extended to add new music functionality that accounts for this, thus allowing calling classes to not worry about the user settings and prevent code repetition.

Playing music

Use the following method from the ResourceService:

public @Null Music playMusic(String musicName, boolean loop)

This method will play the music with the given path, assuming it wasn't playing already. The following will be handled for you:

  • Any previously playing music will be stopped
  • The Music asset will be fetched from the AssetManager and returned if you want to dispose of it later.
  • User-set volume and mute settings will be accounted for.

To stop music, use:

public void stopCurrentMusic()

Calls to the above functions will keep track of which music file is playing, so a call to stopCurrentMusic() will stop the currently-playing music.

Music in the GameArea

The MusicType enum in the MainGameArea stores music types used in-game:

public enum MusicType {
    NORMAL("bgm_new.wav"), BOSS("boss_room_music.mp3");
    ...
}

To add or change music files, simply change these path names or add new enum values. Then call MainGameArea.playMusic(MusicType musicType).

Be sure to reference your asset source in assets/sounds/music/music_citations.txt.