Skip to content

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.

GameApp.addSound("jump", "audio/jump.wav");

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.

GameApp.addMusic("theme", "audio/theme.mp3");

Disposing audio

In the hide() method of your screen you should unload all the audio (both sounds and music).

Sound effects

GameApp.disposeSound("jump");

Music tracks

GameApp.disposeMusic("theme");

Playing audio

Play a sound at full volume

GameApp.playSound("jump");

Play a sound with volume control

Volume is between 0.0 (silent) and 1.0 (full).

GameApp.playSound("jump", 0.5f);

Play music

If music is already playing, it will not restart.

Parameter looping:

  • true = loops forever
  • false = plays once
GameApp.playMusic("theme", true);

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).
GameApp.playMusic("theme", true, 0.5f);

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.

long id = GameApp.playSound("jump");
GameApp.stopSound(id);

Stop all sounds

GameApp.stopAllSounds();

Stop specific music track

When playing music, there is no trackable id provided. You can just stop the music by providing the name.

GameApp.stopMusic("theme");

Stop all music

In case there are multiple music tracks playing at the same time, you can stop all music.

GameApp.stopAllMusic();

Stop Everything (Emergency Stop)

Immediately stop all audio.

GameApp.stopAllAudio();

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 (.wav or .ogg).
  • Use compressed formats for music (.mp3 or .ogg).