Skip to content
Greg Rogers edited this page Jan 4, 2020 · 5 revisions

The Audio library handles loading and caching audio buffers loaded from a file. It also supports playing audio, looping audio, as well as mixing ambient noises (in development).

Loading Sounds

Before playing your sounds, you'll need to load them individually or many by file (preferred). Sounds are loaded via the Web Audio API.

Many Sounds
If you want to have a set (or all) of your sounds loaded at once, say, before starting the render loop, you can call

await Audio.get().loadAllFromFile('path/to/sounds.json');

Where sounds.json is a file containing a manifest of all audio files you would like to load.

{ 
  "example": {
    "extension": "wav",
    "volume": 0.5
  }
}

Individual Sound
In order to load an individual sound, you'll need to call

const sound = await Audio.get().loadSound(directory, name, options);

Playing Sounds

In order to play the sounds you've loaded, simply call

Audio.get().playSound(name);

For continuous looping audio (such as engine sounds, lights humming, etc), you can use

Audio.get().playSoundOnLoop(name);

The Audio library does not support mixing looped audio, so you'll need to make sure your audio loops properly.

Ambient Mixing

Ambient sound mixing is not yet ready for use.

Notes

Clone this wiki locally