Skip to content
Download

Sound

The sounds are WAV, OGG or MP3 files in the project. The name of the sound is the name of the file without the extension.

Function What it does Example
SoundId Audio::play(std::string_view name, PlayOptions options = {}) Starts playing a sound and returns right away. Returns a SoundId, or 0 if the sound doesn’t exist. Audio::play("explosao");
void Audio::stop(SoundId id) Stops a sound that is playing. Audio::stop(music_);
void Audio::stopAll() Stops all the sounds. Audio::stopAll();

PlayOptions has two fields, which you pass between braces:

Field Default What it is
volume 1.0f Volume, from 0 to 1.
loop false true repeats the sound until Audio::stop.
Audio::play("tiro", {.volume = 0.4f});
SoundId music = Audio::play("musica", {.volume = 0.6f, .loop = true});

Background music is read from the file little by little, without loading everything into memory. Only one music plays at a time: playing another one replaces it.

Function What it does Example
bool Audio::playMusic(std::string_view name, float volume = 1.0f, bool loop = true) Starts playing the music. Returns false if the sound doesn’t exist. Audio::playMusic("tema", 0.6f);
void Audio::stopMusic() Stops the music. Audio::stopMusic();
void Audio::setMusicVolume(float volume) Changes the music volume, from 0 to 1. Audio::setMusicVolume(0.3f);
bool Audio::musicPlaying() true if a music is playing. if (!Audio::musicPlaying()) ...
  • Several sounds can play at the same time.
  • Formats: WAV, OGG and MP3. Use WAV for short effects and OGG or MP3 for music.
  • The Copiar código (copy code) button in the sound’s Inspector copies the line Audio::play("...");. See Sounds.