Plugin Howler Audio - BaryoDev/dopaminejs GitHub Wiki

Plugin: Howler Audio

dopaminejs-plugin-howler-audio 1.0.0. Needs howler 2.2 or newer, which you install yourself.

npm install dopaminejs-plugin-howler-audio howler

Registers a kernel system named audio that plays files through Howler. Use it for music, loops and playback rate. For short synthesized sounds the core SoundManager needs no files and no dependency.

import { Game } from 'dopaminejs';
import { HowlerAudioPlugin } from 'dopaminejs-plugin-howler-audio';

const game = new Game();
game.kernel.plugins.use(HowlerAudioPlugin);

const audio = game.kernel.systems.get('audio');

audio.registerSound('coin', '/audio/coin.mp3');
await audio.preloadSounds({ music: '/audio/theme.mp3' });

audio.play('coin');
const id = audio.play('music', { loop: true, volume: 0.4 });

API

Method Notes
registerSound(key, url) Creates the Howl. Loading starts at once.
register(key, { src }) Same, with an options object. url is accepted too.
preloadSounds({ key: url }) Registers each and resolves when all have loaded or failed
play(key, { volume, loop, rate }) Returns the Howler sound id. An unknown key warns.
stop(key) Stops every playing instance of that sound
toggleMute() Returns the new state
setVolume(value) 0 to 1, clamped

Things to know

  • Mute and volume are Howler's global settings. They affect every Howl on the page, including ones this plugin did not create.
  • Mute is not saved. SoundManager saves its own mute flag, and the two do not know about each other.
  • It is separate from SoundManager. The Dopamine facade keeps using SoundManager.
  • If a system named audio exists, the plugin replaces it.
  • It logs to the console on install and removal.

Removing it

game.kernel.plugins.remove('howler-audio');   // unloads every Howl

With dopaminejs 2.4.0 or newer this also unregisters the audio system. On an older core call game.kernel.systems.unregister('audio') too. An audio system the plugin replaced on install is not restored.