Unleash awesomeness. Private packages, team management tools, and powerful integrations. Get started with npm Orgs »

sound-box

0.3.7 • Public • Published

soundbox

A super simple JS library for playing sound effects and other audio.

  • Current size: 1.4kb
  • Current minified size: 0.74kb

Demo

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)

Download

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:

Usage

Create a new instance:

var soundbox = new SoundBox();

Load a sound

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!");
});

Play a sound

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
});

Specify the volume

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);

Specify the default volume

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%

Stop all sounds

You can stop all currently playing sounds like this:

soundbox.stop_all();

Unload a sound

If you want to, you can unload a sound:

soundbox.remove("sound_alias");
soundbox.remove("oops");

Get the current version

Get the current version of SoundBox:

console.log(`Soundbox is at ${SoundBox.version}`);

Changelog

  • 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 and export 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() and default_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
  • v0.3.6: Fix main definition in package.json
  • v0.3.6: Update changelog in README

Real-World Usage

License

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.

install

npm i sound-box

Downloadsweekly downloads

4

version

0.3.7

license

MIT

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
Report a vulnerability