Audio - UQdeco2800/2021-studio-6 GitHub Wiki
Introduction
Audio, e.g. Sounds and music files, can be easily added into the game using libgdx Sound and Music. There is no single audio service, instead audio can be played when and where needed once loaded by the ResourceService
.
Usage
Global Volume Control
For this repository, universal volume control exists to control all music and sound effect (abbreviated sfx). To add your sound / music asset to utilize this, call the following after loading the asset.
- Sound Effect
music.setVolume(ServiceLocator.getResourceService().getSfxVolume());
- Music Tracks (ie. Background Music)
music.setVolume(ServiceLocator.getResourceService().getMusicVolume());
For the implementation, persistent storage of the volume control variables is stored in
UserSettings.java
under theUserSettings.Settings
class. However, this is NOT USED by the game, rather the game loads this variable into global variable withinResourceService.java
singleton, where it is then accessed by thegetMusicVolume()
andgetSfxVolume()
functions.
Example: Sound Effect
- Load sound: Ensure the sound file has been loaded in an active resource batch by the
Resource Service
usingloadSounds()
.
private static final String[] soundsToLoad = {"sounds/sound.ogg"};
resourceService.loadSounds(soundsToLoad);
music.setVolume(ServiceLocator.getResourceService().getSfxVolume());
- Play sound: Retrieve the sound from the
Resource Service
usinggetAsset()
. Configure the playback settings, then useplay()
to play the file.
Sound sound = ServiceLocator.getResourceService().getAsset("sounds/sound.ogg", Sound.class);
music.setVolume(ServiceLocator.getResourceService().getMusicVolume());
sound.play();
Example: Background Music
- Load music: Ensure the music file has been loaded in an active resource batch by the
Resource Service
usingloadMusic()
.
private static final String[] musicToLoad = {"sounds/music.mp3"};
resourceService.loadMusic(musicToLoad );
- Play music: Retrieve the music from the
Resource Service
usinggetAsset()
. Configure the playback settings, then useplay()
to play the music. The following example is set to loop, and to play with volume at 30%.
Music music = ServiceLocator.getResourceService().getAsset("sounds/music.mp3", Music.class);
music.setLooping(true);
music.setVolume(0.3f);
music.play();