Sound and Music
The Saxion GameApp allows you to easily add and control sounds and music in your game. You can load audio files once and play them from anywhere.
| Type | Use For | Memory | Can Loop | Multiple at Once |
|---|---|---|---|---|
| Sound | Short effects (jump, hit, click) | Loaded in memory | ❌ No looping | ✅ Yes |
| Music | Background tracks (theme music) | Streamed from file | ✅ Yes | ❌ Usually one at the time |
Loading Audio
To use sounds and music in your game, you need to load it from a file and store it in the global sound/music registry. This is usually done in the show() method of your screen.
You should store your audio in the folder src/main/resources/audio, so that the files are automatically copied to the executable jar file, during deployment.
Sound effects
Load a Sound Effect and store it under the given name. You load an effect once and can reuse it multiple times. Sound effects are stored in memory. The sound effects should be of uncompressed formats such as .wav and .ogg.
Music tracks
Use for background music. Similarly to the sound effects, the music is stored under the given name. The files are directly played from disk and not stored in memory. Therefore we want to use compressed formats such .mp3 or .ogg.
Disposing audio
In the hide() method of your screen you should unload all the audio (both sounds and music).
Sound effects
Music tracks
Playing audio
Play a sound at full volume
Play a sound with volume control
Volume is between 0.0 (silent) and 1.0 (full).
Play music
If music is already playing, it will not restart.
Parameter looping:
- true = loops forever
- false = plays once
Play music with volume control
If music is already playing, it will not restart.
Parameter looping:
- true = loops forever
- false = plays once
Volume parameter:
- Volume is between 0.0 (silent) and 1.0 (full).
Stopping Audio
Stop a specific sound by id
Each sound that you start will get assigned a unique id. We can stop specific sounds by calling the stopSound(long id) method and providing the id.
Stop all sounds
Stop specific music track
When playing music, there is no trackable id provided. You can just stop the music by providing the name.
Stop all music
In case there are multiple music tracks playing at the same time, you can stop all music.
Stop Everything (Emergency Stop)
Immediately stop all audio.
Notes
- Load sounds once (e.g., in
show()). - Don't forget to dispose sounds in the
hide()method. - Don’t load audio inside the render() method — it slows the game.
- Use short formats for sounds (
.wavor.ogg). - Use compressed formats for music (
.mp3or.ogg).