A super simple JS library for playing sound effects and other audio.
Check it out in action with the Online Demo (see also the Real-World Usage section :D)!
The demo can also be run locally. Clone this repository, and then run the following from the root of this repo:
npm install
npm run demo
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")) /* The .then() is optional of course */
.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%
As of v0.3.7, looping support has been added. Use it like this:
// Promise mode
soundbox.play("rocket", null, 0.8, true)
.then(() => /* .... */);
// Callback mode
soundbox.play("rocket", function() {
// Do stuff
}, null, true); // A null volume = use the default volume. Supported in both modes of operation
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}`);
- v1.0.0: This has to be stable by now! Also switch to
alive-server
in dev dependencies to fix security issues - long live forks! - v0.3.10: Bugfix: Don't skip playback if the volume is set to 0 (#10, thanks @matthewoates!) - v0.3.9: Documentation release. Use badges to show size.
- v0.3.8: Add optional
loop
parameter to.play()
- v0.3.7: Update changelog in README
- v0.3.6: Fix
main
definition inpackage.json
- v0.3.5: Update from
.jsm
to.mjs
for file extension - v0.3.4: Fix another bug in
.stop_all()
- v0.3.3: Fix a bug in
.stop_all()
- v0.3.2: Added
.stop_all()
anddefault_volume
- v0.3.1: Added non-es6 version and build system to automate minification.
- 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.2: Now with button mashing support!
- @MyTheValentinus has created a mobile web app for playing sound-effects and other sounds
- @kdallas has used it as part of a 100 movies list
- ...? (Are you using soundbox? Let me know by opening an issue or contacting me and I'll add your project here!)
Contributions are welcome! Please mention that you release your contribution under the MIT license (see below) in your PR message.
Edits should be made to soundbox.js
- the other 3 are automatically generated by the bash-based build script build
, which is located in the root of this repository.
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.