Dead easy audio
- Permissive MIT license
- Designed both for app UI and game usecases
- Automatic caching and preloading
- Audiofiles are replayable
- Rapid fire sound support
- Play multiple audiofiles simultaneously
- Maintains the user's background music on mobile (a killer feature if you want to allow the user to play their own music or podcasts while playing your app/game)
- Promise support
- npm/browserify/webpack and global browser module support
- Desktop and mobile browsers support
- Chaining support
- Demo included
es6-promiseare good polyfills
whatwg-fetchis a good polyfill
- Web Audio API : Native support for the Web Audio API is required in order to produce the audio
Install with npm:
npm install audiofile
Alternatively, you can just download
audiofile.js and use
The library exposes the
Audiofile object, from which you can create instances:
var af = /* options... */ ;
The use of the new keyword is optional, so you could instead do:
var audiofile = ;var af = ;
options object can contain these fields:
url: The URL of the audio (mandatory if using
fetch: A fetch init options object, allowing you to customise the request headers et cetera (fetch defaults are default here, i.e. plain HTTP GET).
load: An alternative to
fetch. When specified,
fetchis ignored the
loadpromise is used to get the file. The promise is expected to resolve to a
preload: Whether or not to load the audio on object creation (default
loop: Whether to infinitely loop the audio (default
volume: The volume of the audio in percent (0 to 1 inclusive; default
1). Values greater than 1 boost the volume.
output: Redirects the output of the audiofile to the specified
AudioNode, allowing for postprocessing of the audio (system audio is the default).
If you want to disable the user's own background audio (e.g. music, podcasts), then you must call this function. This is useful in games with their own background music. By default, the user's background audio is respected.
You may alternatively call this:
af.clone() returns a new audiofile that has the same cached audio as the calling audiofile. The cloned audiofile can be played independently of the calling audiofile. This is especially useful for rapid fire usecases.
var af2 = af;
af.load() returns a promise that is fulfilled when loading is completed. It's useful if you want to load the audio at a particular time, and it's generally useful for detecting when you can play.
If you want a quick synchronous check if the audiofile is loaded or not, just read
Play the audiofile at its current progress level.
Pause the audiofile at its current progress level. It can be resumed via
Stop playback of the audiofile and resets its progress to the beginning.
Get or set the progress of the audiofile in milliseconds.
var time = af; // get progressaf; // progress twice as far
Set the progress of the audiofile relatively in milliseconds. Positive values fastforward the audiofile, and negative values rewind the audiofile.
af; // fastforward 1 second
There are also aliases:
af; // same as af.progressDelta( -1000 )af; // same as af.progressDelta( 1000 )
Get or set the volume level as a percent (0 to 1 inclusive). Values greater than 1 boost the volume.
var vol = af; // current levelaf; // set level to 50%
Get or set whether looping is enabled. This is useful if you want to reconfigure an audiofile.
var looping = af;af; // turn off looping