Game Development Reference
In-Depth Information
At this point, it would seem logical to just call play() on the sound and music
instances in the game code where needed. However, this approach poses an issue
in terms of clean code with regard to the game settings as the current game settings
need to be checked every time an audio file is played. So, it would be ideal to have
an audio manager as a centralized point of control over the game's audio playback.
Since LibGDX does not provide an audio manager, we will build one of our own.
Create a new file for the AudioManager class and add the following code:
package com.packtpub.libgdx.canyonbunny.util;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.audio.Sound;
public class AudioManager {
public static final AudioManager instance = new AudioManager();
private Music playingMusic;
// singleton: prevent instantiation from other classes
private AudioManager () { }
public void play (Sound sound) {
play(sound, 1);
}
public void play (Sound sound, float volume) {
play(sound, volume, 1);
}
public void play (Sound sound, float volume, float pitch) {
play(sound, volume, pitch, 0);
}
public void play (Sound sound, float volume, float pitch,
float pan) {
if (!GamePreferences.instance.sound) return;
sound.play(GamePreferences.instance.volSound * volume,
pitch, pan);
}
}
 
Search WWH ::




Custom Search