Code Monkey home page Code Monkey logo

quicksound.js's Introduction

quicksound.js

A very simple JavaScript sound library using the Web Audio API and a fallback to flash. On the desktop it works with any browser that supports either Web Audio API or the flash fallback. On mobile it works with Chrome or Firefox for Android, and iOS Safari as of iOS 6. See the following for Web Audio API support:

http://caniuse.com/#feat=audio-api

To use this library, download the minified (or non-minified) JS in the release directory, as well as the swf file which must be placed next to the HTML in which you include the JS.

Example

quicksound.create(function() {
  quicksound.load({
    path: 'sound.mp3',
    doneFunc: function(_, sound) {
      quicksound.play(sound);
    },
    errorFunc: function() {
      console.log('Failed to load sound');
    }
  });
});

Usage

quicksound.create(doneFunc, errorFunc)

This initializes the quicksound library. If WebAudio is available, that will be used, otherwise a flash fallback will be used. If the library is successfully initialized, doneFunc will be called, otherwise errorFunc will be called.

Before the create function is called, the other functions are not available.

quicksound.bestFormat(formats)

Checks for the browser support of different audio formats. Pass an array of formats (using the common file extensions) and bestFormat will return the first supported one, or null if none is supported. If the flash fallback is used, only mp3 will be supported.

quicksound.bestFormat(['aac','ogg','mp3','wav'])
// -> 'ogg'

quicksound.load(options)

This loads one or more sounds from specified paths. The options object is expected to contain the following properties:

  • path or paths: a string, array or object that defines where to load the sounds from.
  • doneFunc: callback when all sounds are successfully loaded. Parameters are the options object and result object.
  • errorFunc: callback if one or more sounds were not successfully loaded. Parameters are the options object and if at least one sound was successfully loaded the result object.

Optional options:

  • extension: the file extension to use for each given path
  • retries: the number of times the libary will retry loading the resource from the server (default 3)

See the examples for usage:

quicksound.load({
  path: 'laser.ogg',
  doneFunc: function(options, sound) {},
  errorFunc: function(options) {}
});
// doneFunc will get a single sound object

quicksound.load({
  path: [ 'laser.ogg', 'boom.ogg' ],
  doneFunc: function(options, result) {},
  errorFunc: function(options, result) {}
});
// doneFunc will get an object in the form: { 'laser.ogg': sound1, 'boom.ogg': sound2 }

quicksound.load({
  path: { shot: 'laser', explosion: 'boom' },
  extension: '.ogg',
  doneFunc: function(options, result) {},
  errorFunc: function(options, result) {}
});
// doneFunc will get an object in the form: { 'shot': sound1, 'explosion': sound2 }

quicksound.play(sound, [offset, loop])

This plays a given sound. Optional parameters are the offset (in seconds) and a boolean if the sound should loop.

quicksound.stop(sound)

Stop playing a particular sound

quicksound.js's People

Contributors

tfriedel6 avatar

Stargazers

 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.