Skip to content

Playing Music & Sounds

Walkthrough

Follow these simple steps to integrate sound into your Turbo game:

Create an audio folder

Inside your project directory, create a folder named audio. This folder will contain all your sound files.

your-project-dir/       # Your project's root directory.
β”œβ”€β”€ audio/              # The directory of your audio assets. 
β”œβ”€β”€ src/                # The directory of your code.
β”‚   └── lib.rs          # The main file for the game.
β”œβ”€β”€ Cargo.toml          # Rust project manifest.
└── turbo.toml          # Turbo configuration.

Put audio files in the folder

The following file formats are supported: .wav, .mp3, .ogg, and .flac.

Make some noise

One-Shot Sound Effects

Use audio::play to play a sound effect from your audio folder. For example, if your sound effect is named coin.wav:

audio::play("coin")

Looping Audio Tracks

You can loop audio by checking if the audio track has stopped playing and restarting it if so:

if !audio::is_playing("background_music") {
    audio::play("background_music");
}

Pausing and Stopping

Pausing a track, will stop it from playing. When resumed with audio::play, it will continue from the point at which it was paused.

audio::pause("background_music");

Pausing a track, will stop it from playing. When resumed with audio::play, it will restart from the beginning.

audio::stop("background_music");