AudioManager
Play sounds using Web Audio, fallback on HTML5 Audio.
AudioManager
is specifically designed to works for games that have a big
quantity of audio assets. Loading and unloading is made easy and transparent.
If available, WizAsset is used for downloading files to disc.
API
var audioManager = new AudioManager(channelIds);
audioManager.init();
audioManager.setVolume(channelId, volume);
audioManager.playSound(channelId, soundId, volume, panoramic, pitch);
audioManager.createSoundGroups(soundGroupDefs, channelId);
audioManager.createSoundGroup(soundGroupId, soundGroupDef, muted);
audioManager.playSoundGroup(channelId, groupId, volume, panoramic, pitch);
audioManager.playLoopSound(channelId, soundId, volume);
audioManager.stopLoopSound(channelId);
audioManager.stopAllLoopSounds();
audioManager.release();
Documentation
Create audioManager object and channels
Pass the list of channels to the constructor as an array of strings.
var channels = ['music', 'sfx', 'ui'];
var audioManager = new AudioManager(channels);
Setup audioManager path to sound assets folder
audioManager.settings.audioPath = 'assets/audio/';
Start audio engine.
To work correctly on iOS, this must be called on an user interaction
e.g. user pressing a button.
(C.f. this page to understand how this work.)
gameStartButton.on('tap', function () {
audioManager.init();
});
Set channel volume
By default, channel volume is set to 0 and channel is muted.
No sounds will play until channel volume is set.
var volume = 1.0;
audioManager.setVolume('ui', volume);
Create and play a simple sound.
Create a sound and playing it in a channel.
Sound is created and loaded automatically.
var fileName = 'laser1';
audioManager.playSound('sfx', fileName);
var volume = 0.7;
var panoramic = 0.9;
audioManager.playSound('sfx', fileName, volume, panoramic);
Sounds creation and preloading can be forced.
audioManager.createSound(fileName).load();
Alternatively, sounds can be played outside channels.
var sound = audioManager.createSound(fileName);
sound.play(volume, panoramic, pitch);
Change pitch
This feature is only available with WebAudio.
var pitch = -7.0;
sound.setPitch(pitch);
The pitch can be set at play.
audioManager.playSound('ui', fileName, volume, panoramic, pitch);
While a sound is playing, the pitch can be changed dynamically
var portamento = 3.0;
sound.setPitch(pitch, portamento);
Create and play sound groups.
A sound group is a collection of sounds that will play alternatively in a
round-robin pattern on each play
call.
var soundGroupDef = {
id: ['punch1', 'punch2'],
vol: [1.0, 0.8],
pitch: [0.0]
};
audioManager.createSoundGroups('punch', soundGroupDef);
var volume = 0.8;
var panoramic = 0.3;
var pitch = 3.0;
audioManager.playSoundGroup('sfx', 'punch', volume, panoramic, pitch);
You can create several sound groups in one function call
var soundGroupDefs = {
groupId1: { id: ['sound1', 'sound2'], vol: [1.0, 0.8], pitch: [0.0] },
groupId2: { ... },
...
};
audioManager.createSoundGroups(soundGroupDefs, 'sfx');
Play and stop looped sounds
Only one loop can play per channel. Playing a new looped sound in the same
channel will stop current playing sound before starting new one.
var volume = 1.0;
var fileName = 'bgm1';
audioManager.playLoopSound('music', fileName, volume);
audioManager.stopLoopSound('music');
audioManager.stopAllLoopSounds();
How looped sound fades and mixes can be set by modifying audioManager settings:
audioManager.settings.defaultFade = 3;
audioManager.settings.crossFade = true;
Release memory
audioManager.release();