A super simple JS library for playing sound effects and other audio.
- Current size:
1.4kb
- Current minified size:
0.74kb
Quick example:
var soundbox = new SoundBox();
soundbox.load("beep-a", "beep-a.wav")
.then(
() => console.log("Loaded beep a!"),
() => console.error("Failed to load keep a :-(")
);
soundbox.load("beep-b", "beep-b.wav");
soundbox.load("beep-c", "beep-c.wav");
soundbox.load("victory", "victory.mp3");
soundbox.play("beep-a")
.then(() => soundbox.play("beep-b"))
.then(() => soundbox.play("beep-c"))
.then(() => soundbox.play("victory"));
// The 3rd parameter is the volume: The callback can be omitted to enable promise mode
soundbox.play("beep", null, 0.8)
.then(() => soundbox.play("victory"))
(Full documentation below)
The recommended way to obtain soundbox is via npm here: sound-box - To install it simply type npm install sound-box
. Then you can include any of the 4 files described below from the ./node_modules/sound-box/
directory.
If npm isn't for you, then the latest master should be perfectly stable. If you want something that you know will work, try the latest release.
The following files are available via all release methods:
- soundbox.js - The main development version.
- soundbox.min.js - A minified version of the above.
- soundbox.jsm - An ES6 module-compatible version that's autogenerated. If you possibly can, I'd advise learning about about them. This is a useful resource.
- soundbox.jsm - A minified version of the above.
var soundbox = new SoundBox();
Sound loading is done as follows. If a callback is not specified, promise mode is activated.
// Promise mode
soundbox.load("alias", "path/to/filename").then(
() => console.log("Loaded beep!"),
() => console.error("Failed to load beep :-(")
);
soundbox.load("victory", "./sounds/yay.wav").then( /* .... */);
// Callback mode
soundbox.load("alias", "path/to/filename", function() {
console.log("Loaded beep!");
});
Similarly, playing a sound takes 2 forms: callback and promise.
// Promise mode
soundbox.play("beep-a")
.then(() => soundbox.play("beep-b"))
.then(() => soundbox.play("beep-c"))
.then(() => soundbox.play("victory"));
// Callback mode
soundbox.play("beep", function() {
// Do stuff
});
The volume can be specified when playing a sound, too. The volume should be a floating-point number between 0 (silent) and 1 (full volume, default).
Here's how:
// Promise mode
soundbox.play("beep-a", null, 0.8)
.then(() => /* .... */);
// Callback mode
soundbox.play("beep", function() {
// Do stuff
}, 0.5);
If you'd like to change the default volume that sounds are played at, do this:
soundbox.default_volume = 0.45; // Sets the default volume to 45%
You can stop all currently playing sounds like this:
soundbox.stop_all();
If you want to, you can unload a sound:
soundbox.remove("sound_alias");
soundbox.remove("oops");
Get the current version of SoundBox:
console.log(`Soundbox is at ${SoundBox.version}`);
- v0.2: Now with button mashing support!
- v0.3: Added volume support! Converted for use as an ES6 module. If this doesn't suit you, then just remove the
import
andexport
statements to make it the way it was before. - v0.3.1: Added non-es6 version and build system to automate minification.
- v0.3.2: Added
.stop_all()
anddefault_volume
- v0.3.3: Fix a bug in
.stop_all()
- v0.3.4: Fix another bug in
.stop_all()
- v0.3.5: Update from
.jsm
to.mjs
for file extension
- @MyTheValentinus has created a mobile web app for playing sound-effects and other sounds
- ...? (Are you using soundbox? Let me know by opening an issue or contacting me and I'll add your project here!)
SoundBox.js is licensed under MIT:
The MIT License (MIT)
Copyright (c) 2015 Starbeamrainbowlabs
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.