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 the UserSettings.Settings class. However, this is NOT USED by the game, rather the game loads this variable into global variable within ResourceService.java singleton, where it is then accessed by the getMusicVolume() and getSfxVolume() functions.

Example: Sound Effect

  1. Load sound: Ensure the sound file has been loaded in an active resource batch by the Resource Service using loadSounds().
private static final String[] soundsToLoad = {"sounds/sound.ogg"};
resourceService.loadSounds(soundsToLoad);
music.setVolume(ServiceLocator.getResourceService().getSfxVolume());
  1. Play sound: Retrieve the sound from the Resource Service using getAsset(). Configure the playback settings, then use play() to play the file.
Sound sound = ServiceLocator.getResourceService().getAsset("sounds/sound.ogg", Sound.class);
music.setVolume(ServiceLocator.getResourceService().getMusicVolume());
sound.play();

Example: Background Music

  1. Load music: Ensure the music file has been loaded in an active resource batch by the Resource Service using loadMusic().
private static final String[] musicToLoad = {"sounds/music.mp3"};
resourceService.loadMusic(musicToLoad );
  1. Play music: Retrieve the music from the Resource Service using getAsset(). Configure the playback settings, then use play() 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();