Class SoundEffectManager

java.lang.Object
edu.cornell.gdiac.audio.SoundEffectManager
All Implemented Interfaces:
SoundEffect.OnCompletionListener

public class SoundEffectManager extends Object implements SoundEffect.OnCompletionListener
A singleton class for controlling sound effects in LibGDX

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 Details

    • getInstance

      public static SoundEffectManager getInstance()
      Returns the single instance for the SoundEffectManager

      The first time this is called, it will construct the SoundEffectManager.

      Returns:
      the single instance for the SoundEffectManager
    • play

      public boolean play(String key, SoundEffect sound)
      Plays the an instance of the given sound

      A 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 instance
      sound - The buffer with the sound data
      Returns:
      True if the sound was successfully played
    • play

      public boolean play(String key, SoundEffect sound, float volume)
      Plays the an instance of the given sound

      A 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 instance
      sound - The buffer with the sound data
      volume - The sound volume in the range [0,1]
      Returns:
      True if the sound was successfully played
    • play

      public boolean play(String key, SoundEffect sound, float volume, boolean loop)
      Plays the an instance of the given sound

      A 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 instance
      sound - The buffer with the sound data
      volume - The sound volume in the range [0,1]
      loop - Whether to loop the sound
      Returns:
      True if the sound was successfully played
    • stop

      public void stop(String key)
      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

      public boolean isActive(String key)
      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

      public void onCompletion(SoundEffect effect, long instance)
      Called when the end of a music stream is reached during playback.
      Specified by:
      onCompletion in interface SoundEffect.OnCompletionListener
      Parameters:
      effect - The sound effect that finished playing
      instance - The particular instance that has completed