In my tenth video of the “Building a Game Development Framework” series, I focus on integrating audio capabilities into the LibGDX framework. I cover the distinction between sound effects and music, how to manage their lifecycles, and how to trigger them within an animation system.
The LibGDX framework distinguishes between two types of audio objects based on how they are handled in memory:
- Sound: Intended for short clips (typically < 10 seconds), such as explosions or footsteps. These are completely loaded into RAM.
- Music: Intended for longer tracks, like background music or ambient loops. These are streamed from the storage device to save memory.
I demonstrate the primary methods for controlling both types of audio:
- Playback: Simple
.play(),.pause(),.resume(), and.stop()methods. Note thatMusicdoes not have a dedicated “resume” method; callingplay()on a paused track automatically resumes it from where it left off. - Volume & Pitch: Volume is set as a float from
0.0to1.0. Pitch (available only forSound) speeds up or slows down the audio to change its tone—useful for varied sound effects like different-pitched “hellos”. - Panning: Moves audio between the left and right speakers (values from
-1.0to1.0). This only works on mono audio files. - Positioning: Specifically for
Music, you can jump to a specific second in a track usingsetDevicePosition().
Because audio objects consume system resources, they must be “disposed” when no longer needed.
- Automatic Disposal: I have updated the
BaseScreenwith maps for bothSoundandMusicobjects. When a screen is closed, the framework automatically iterates through these maps and disposes of every audio file to prevent memory leaks.
Then I update my framework to have “Sound-Aware” animations. By adding a Sound field to the BaseAnimation class, animations can now trigger audio automatically:
- Static/Block Animations: Play a sound once when they first appear on the screen.
- Loop Animations: Play a sound at the start of every loop cycle (e.g., a footstep sound every time a walk cycle reaches the first frame).
- Bounce Animations: Trigger sounds at both the beginning and the “apex” (last frame) of the animation, making it ideal for things like bouncing balls or swinging pendulums.
- Random Animations: Use a counter to ensure a sound plays every time the animation completes a full set of frames, even though the frames themselves are randomized.
I then recommend two browser-based tools for generating 8-bit/16-bit sound effects for game development:
- SFXR: A classic generator for “retro” sounds.
- ChipTone: A more modern, user-friendly version of SFXR for creating explosions, jumps, and other game effects.
