Skip to content

Guide: Playing sound effects & music

Alan Stagner edited this page Jun 16, 2020 · 2 revisions

BNA internally uses the SoLoud sound library. Sound effects can be conveniently loaded from disk as WAVs, FLACs, OGGs, or MP3s & played back either in 2D or 3D, and can also be played as background songs.

You can load a sound effect from disk, either in-memory or streaming:

using BNA.Audio;

// ...

SoundEffect mySoundEffect;
if(SoundEffect.Load("./Content/Audio/mySound.ogg") case .Ok(let sfx)) // or LoadStreaming if you want to stream from disk instead of reading & decompressing the whole file in at once
{
    mySoundEffect = sfx;
}

And then play it, getting a handle to the new playing instance:

let sfxHandle = Audio.Play( mySoundEffect ); // or Play3D

You can also construct a Song, which wraps two different SoundEffects: the first sound effect is optional and is a non-looping intro portion to play. The second sound effect is required, and is a looping portion to be "stitched" onto the intro portion and repeats forever afterwards.

let song = Song(myIntroSoundEffect, myLoopSoundEffect);
let songHandle = Audio.PlaySong( song );