• Stars
    star
    111
  • Rank 314,510 (Top 7 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 13 years ago
  • Updated almost 9 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A Simple HTML5 Audio Helper Library

Javascript AudioFX (v0.4.0)

Simple HTML5 <audio> support.

  • Easily play() and stop() any audio track.
  • Add support for audio pools for short, repeating, overlapping sounds.
  • Add support for loop in browsers that don't natively support it.
  • Load appropriate audio format based on current browser support (ogg|mp3|m4a|wav)
  • Callback when audio has loaded and canplay

You can find an article about how the library works here

Download

You can download audio-fx.js, or get the minified version here

Alternatively:

git clone [email protected]:jakesgordon/javascript-audio-fx
  • All code is in audio-fx.js
  • Minified version available in audio-fx.min.js
  • Less than 1.2K (minified)
  • No 3rd party library is required
  • Demo can be found in /index.html

Usage

Include audio-fx.js in your application.

In its simplest form, load music by constructing an AudioFX object:

var music = AudioFX('sounds/music.mp3');

The AudioFX instance has the simplest possible API:

music.play();     // play the audio
music.stop();     // stop the audio
music.audio;      // the underlying <audio> tag

However, different browsers support different formats so you should provide at least ogg and mp3 formats if you want to support a broad range of browsers:

var music = AudioFX('sounds/music', { formats: ['ogg', 'mp3'] });

Other options include volume, loop and autoplay:

var music = AudioFX('sounds/music', {
                      formats: ['ogg', 'mp3', 'm4a', 'wav'],
                      volume:   0.5,
                      loop:     true,
                      autoplay: true });

Audio Pools

If you want to play a short sound effect multiple overlapping times (e.g. game explosions and effects) then you need multiple <audio> tags.

The AudioFX library provides this functionality by accepting a pool option:

var shoot = AudioFX('sounds/shoot', { formats: ['ogg', 'mp3'], pool: 10 });

The AudioFX instance has the exact same API as above:

music.play();     // play the first available audio
music.stop();     // stop the audio
music.audio;      // an ARRAY containing the pool of underlying <audio> tag

Waiting for Sounds to Load

The AudioFX method accepts an optional 3rd parameter that can contain a function that will be called when the <audio> tag has loaded:

var name    = 'sounds/music';
var options = { formats: ['ogg', 'mp3'] };
var onload  = function() { alert('music is ready'); }

var music = AudioFX(name, options, onload);

BrowserSupport

If necessary, you can examine the AudioFX.supported property to find out what kind of <audio> support is provided by your browser.

It will be false if there is no support at all, otherwise it will contain:

AudioFx.supported.ogg;  // (true|false)
AudioFx.supported.mp3;  // (true|false)
AudioFx.supported.m4a;  // (true|false)
AudioFx.supported.wav;  // (true|false)
AudioFx.supported.loop; // (true|false) - some browsers dont (yet) support the loop option

NOTES

After starting this project, I discovered the buzz library which also abstracts HTML5 audio functionality... and does it much more thoroughly than audio-fx but does not have support for creating an audio pool.

... I should probably just be forking buzz and trying to add pooling support to it, but I haven't had the time yet. If you have any thoughts let me know !

Also note that browser support for HTML5 audio is still pretty messed up. This library works pretty well in most modern desktop browsers, IE9, Chrome13, FF5, Opera11, but Safari continues to have delays that make short sounds unusable. Also mobile browsers are very limited in their support for HTML5 Audio.

You can find an example rant here.

So... Very Early experimentation. YMMV

License

MIT license.

Sample Sounds

NOTE: the sample sounds included in this project are royalty free sounds licensed from Lucky Lion Studios and Premium Beat. They are licensed ONLY for use in this project. If you fork this code you must provide your own sample sounds.

Thanks for your honesty!

Contact

If you have any ideas, feedback, requests or bug reports, you can reach me at [email protected], or via my website: Code inComplete

More Repositories

1

javascript-state-machine

A javascript finite state machine library
JavaScript
8,690
star
2

javascript-racer

How to build an outrun-style racing game in javascript
HTML
1,033
star
3

bin-packing

A javascript binary tree based algorithm for 2d bin-packing suitable for generating CSS sprites
JavaScript
628
star
4

sprite-factory

Automatic CSS Sprite Generator
Ruby
620
star
5

javascript-tetris

A simple javascript tetris game
HTML
586
star
6

javascript-pong

A javascript version of the classic 1972 arcade game Pong!
JavaScript
202
star
7

javascript-tiny-platformer

A very minimal javascript platform game
JavaScript
172
star
8

javascript-gauntlet

An HTML5 Gauntlet-style game
JavaScript
155
star
9

javascript-boulderdash

An HTML5 implementation of the c64 classic Boulderdash game
JavaScript
103
star
10

javascript-snakes

An HTML5 Snakes Game
HTML
95
star
11

javascript-tower-platformer

A prototype for an html5 rotating tower platformer game
JavaScript
86
star
12

javascript-breakout

An HTML5 canvas implementation of breakout
JavaScript
72
star
13

rack-rabbit

A Unicorn-style forking server for hosting rabbitMQ consumers as load balanced Rack apps
Ruby
62
star
14

javascript-starfield

An old school 2d starfield using HTML5 <canvas>
JavaScript
46
star
15

javascript-delta

An HTML5 javascript shoot em up in the style of the old c64 classic game "Delta"
JavaScript
43
star
16

ruby-sample-daemon

A simple example of daemonizing a long running Ruby process
Ruby
13
star
17

tetris-shoes

A Tetris game in Ruby Shoes
Ruby
5
star
18

javascript-stutter

Can anyone make this minimalist canvas animation smooth ?
JavaScript
3
star
19

dotfiles

Personal dotfiles
Vim Script
2
star
20

sprite-factory-and-rails31

Experimenting with sprite-factory and the rails 3.1 asset pipeline
Ruby
2
star
21

snowpack-coverage-bug

Reproducing a snowpack/web-test-runner coverage issue
JavaScript
1
star
22

unified-assets

Easily unify and minify static javascript and css assets
Ruby
1
star
23

vue-template-brunch

Adds support to Brunch for pre-compiling standalone (NOT single-file) vue templates.
JavaScript
1
star
24

ruby-serializer

Declarative serialization of PORO's to JSON
Ruby
1
star