Zap! is an audio manager for html 5 applications with a focus on real-time uses such as games.
It provides a common interface that works across the individual browser quirks and shortcomings such as multiple channels, looping and callbacks.
Zap! depends on jQuery 1.4+
Init should be run before anything else to setup configuration and create dom elements
Arguments
- options - Optional configuration object
Example
Zap.init({
console: true,
container: 'sound-holder'
});
There are currently three events that can be bound using the 'on' function; 'success', 'update' and 'error'. Multiple callbacks may also be bound.
Arguments
- ref - An event (success, update or error)
- callback - A function to be called whenever the above event occurs
Example
Zap.on('complete', function(){
// all sounds are loaded
});
Zap.on('update', function(percent){
// sounds are loading
});
Zap.on('error', function(ref){
// there was a problem loading a sound file
});
Add a sound to be loaded, this method can be chained.
Arguments
- ref - A unique string reference for this sound
- sources - An array of possible sources in the order they should be checked
- channels - The maximum number of channels available for this sample
- time - Time to wait until giving up on the sound loading
Example
Zap.addSound('beat', ['beat.mp3', 'beat.ogg', 'beat.wav'])
.addSound('laser', ['shoot.mp3', 'shoot.ogg'], 5);
Allows sounds to be added into groups, groups can then be played back like individual sounds - a random sound from the group will be chosen. This method can be chained.
Arguments
- ref - A unique string reference for this group
- sounds - An array of possible sound references
Example
Zap.addGroup('punch', ['thud', 'pow', 'kablam']);
Zap.addSound('thud', ['thud.mp3', 'thud.ogg']),
.addSound('pow', ['pow.mp3', 'pow.ogg']),
.addSound('kablam', ['kablam.mp3', 'kablam.ogg']);
Play a sound by reference, this method can be chained.
Arguments
- ref - The sound reference
- volume - A number between 0-1
- loops - The number of times the sample should loop
- callback - An optional callback to be run once the sample finishes
Example
Zap.play('mysound', 1, 10, function(){
alert('My sound has finished');
});
Stop a sound by reference, this method can be chained.
Arguments
- ref - The sound reference
Example
Zap.stop('mysound');
Stop every loaded sound
Example
Zap.stopAllSounds();
Utility method to find out which formats and codecs are supported in the current browser
Example
Zap.supported();
Releases are available for download from GitHub.
Development: zap.js
Production: zap.min.js