Class SoundEffectManager
- All Implemented Interfaces:
SoundEffect.OnCompletionListener
Sound is a mess in LibGDX for three reasons. (1) You have to keep track of a mysterious number every time you play a sound. (2) You have no idea when a sound has finished playing. (3) OpenAL bugs on macOS cause popping and distortions if you have no idea what you are doing. If you are using the GDIAC audio classes, then those solve (2) and (3). This class is a potential solution for (1).
To get around (1), this sound controller uses a key management system. Instead of waiting for a number after playing the sound, you give it a key ahead of time. The key allows you to identify different instances of the same sound.
Note that this class is only designed for processing SoundEffect
objects only. It is not designed for music as music does not have
simultaneous instances.
-
Method Summary
Modifier and TypeMethodDescriptionstatic SoundEffectManagerReturns the single instance for the SoundEffectManagerbooleanReturns true if the sound instance is currently activevoidonCompletion(SoundEffect effect, long instance) Called when the end of a music stream is reached during playback.booleanplay(String key, SoundEffect sound) Plays the an instance of the given soundbooleanplay(String key, SoundEffect sound, float volume) Plays the an instance of the given soundbooleanplay(String key, SoundEffect sound, float volume, boolean loop) Plays the an instance of the given soundintsize()Returns the number of sounds currently playingvoidStops the sound, allowing its key to be reused.
-
Method Details
-
getInstance
Returns the single instance for the SoundEffectManagerThe first time this is called, it will construct the SoundEffectManager.
- Returns:
- the single instance for the SoundEffectManager
-
play
Plays the an instance of the given soundA sound is defined by a sound buffer, which is the Linear PCM data loaded entirely into memory. You can have multiple instances of the same sound buffer playing. You use the key to identify a sound instance. You can only have one key playing at a time. If a key is in use, the existing sound will be stopped to make way for this one.
- Parameters:
key- The identifier for this sound instancesound- The buffer with the sound data- Returns:
- True if the sound was successfully played
-
play
Plays the an instance of the given soundA sound is defined by a sound buffer, which is the Linear PCM data loaded entirely into memory. You can have multiple instances of the same sound buffer playing. You use the key to identify a sound instance. You can only have one key playing at a time. If a key is in use, the existing sound will be stopped to make way for this one.
- Parameters:
key- The identifier for this sound instancesound- The buffer with the sound datavolume- The sound volume in the range [0,1]- Returns:
- True if the sound was successfully played
-
play
Plays the an instance of the given soundA sound is defined by a sound buffer, which is the Linear PCM data loaded entirely into memory. You can have multiple instances of the same sound buffer playing. You use the key to identify a sound instance. You can only have one key playing at a time. If a key is in use, the existing sound will be stopped to make way for this one.
- Parameters:
key- The identifier for this sound instancesound- The buffer with the sound datavolume- The sound volume in the range [0,1]loop- Whether to loop the sound- Returns:
- True if the sound was successfully played
-
stop
Stops the sound, allowing its key to be reused.If there is no sound instance for the key, this method does nothing.
- Parameters:
key- The sound instance to stop.
-
isActive
Returns true if the sound instance is currently active- Parameters:
key- The sound instance identifier- Returns:
- true if the sound instance is currently active
-
size
public int size()Returns the number of sounds currently playing- Returns:
- true number of sounds currently playing
-
onCompletion
Called when the end of a music stream is reached during playback.- Specified by:
onCompletionin interfaceSoundEffect.OnCompletionListener- Parameters:
effect- The sound effect that finished playinginstance- The particular instance that has completed
-