What is SoundManager?
SoundManager is a Javascript Sound API which talks to Flash, effectively mapping most of Flash 8's native sound capabilities to Javascript. It allows web developers and front-end engineers to programmatically control sound in a cross-browser/platform way using Javascript.
Demos!
Some examples of Javascript-driven sound and sound effects - less talk, more show:
Download
Current version: 2.0a.20060904
SoundManager v2.0a.20060904 (~500 KB, .zip)
Basic Use
In the simplest case, a sound can be created, loaded and played as soon as possible with the minimal required parameters.
soundManager.play('mySound','/path/to/some.mp3');
Sounds can also be created without automatically loading or playing, for later use:
soundManager.createSound('mySound','/path/to/some.mp3');
Once defined, sound objects have methods and properties which can be accessed by the soundManager controller.
soundManager.play('mySound');
soundManager.setVolume('mySound',50);
soundManager.setPan('mySound',-100);
Object Literal Format
Sounds can be created with instance-specific parameters in an object literal format, where omitted parameters inherit default values as defined in soundManager.
soundManager.createSound({
id: 'mySound',
url: '/path/to/some.mp3',
autoLoad: true,
autoPlay: false,
onload: function() {
alert('The sound '+this.sID+' loaded!');
},
volume: 50
});
SoundManager API
The following are methods, properties, event handlers and collections provided by the globally-scoped soundManager Javascript object, and can be set on a global (inherited) default, or per-sound basis.
Methods
- createSound(object:options)
- Creates a sound with an arbitrary number of optional arguments..
- Example:
soundManager.createSound({
id: 'mySound',
url: '/audio/mysoundfile.mp3',
volume: 50,
autoPlay: true,
onprogress: soundIsLoading
});(Note: Code formatting is stylistic, not necessarily recommended.) See Object Literal Format.
- Example:
- createSound(string:id,string:url)
- Creates a sound with the specified ID and URL (simple method.)
- Example:
soundManager.createSound('mySound','/audio/mysoundfile.mp3'); - play(string:id)
- Starts playing the sound specified by ID. (Will start loading if applicable, and will play ASAP.)
- Example:
soundManager.play('mySound'); - stop(string:id)
- Stops playing the sound specified by ID.
- Example:
soundManager.stop('mySound'); - getSoundById(string:id)
- Returns an
SMSoundobject specified by ID. - Example:
soundManager.getSoundById('mySound');
Properties
- autoLoad
- Enable automatic loading (otherwise .load() will be called on demand with .play(), the latter being nicer on bandwidth - if you want to .load yourself, you also can)
- autoPlay
- Enable playing of file as soon as possible (much faster if "stream" is true)
- stream
- Allows playing before entire file has loaded (recommended)
- multiShot
- Let sounds "restart" or layer on top of each other when played multiple times, rather than one-shot/one at a time
- pan
- Balance settings, left-to-right, -100 to 100
- volume
- Self-explanatory. 0-100, the latter being the max.
Event Handlers
- onid3
- Callback function for "ID3 data is added/available"
- onload
- Callback function for "load finished"
- onprogress
- Callback function for "download progress update" (X of Y bytes received)
- onplay
- Callback for "play" start
- whileplaying
- Callback for "playing active" (position)
- onstop
- Callback for "user stop"
- onfinish
- Callback function for "song finished playing"
Object Collections
- sounds
- a "hash"/object literal of
SMSoundobjects by ID, used internally by SoundManager. - soundIDs
- an array of sound ID strings, ordered by creation.
Default Options
The soundManager.defaultOptions object sets parameters which are inherited by default when sounds are created. They can be overridden on a per-sound basis at create time, or changed dynamically. Unless options are specified when creating sounds, they will inherit defaults.
defaultOptions apply the properties and event handlers as specified above. Defaults are provided below as an example.
soundManager.defaultOptions = {
debugMode: true,
autoLoad: false,
stream: true,
autoPlay: false,
onid3: null,
onload: null,
onprogress: null,
onplay: null,
whileplaying: null,
onstop: null,
onfinish: null,
multiShot: true,
pan: 0,
volume: 100
}
SoundManager onload equivalent
This is a user-defined onload-style function implemented by SoundManager. Because of the overhead associated in creating and initialising the Flash and Javascript code necessary for SoundManager to function, a separate "onload"-type function is provided so your code can hook into it.
function soundManagerLoad() {
// soundManager should be ready to use/call at this point
soundManager.createSound('mySound','/path/to/mysoundfile.mp3');
soundManager.play('mySound');
}
Demo Credits
Background Tile
Modified from squidfingers.com (free patterns)
Sounds - MPC Demo
Sounds downloaded from AKAI.com
General
SoundManager was written to meet a desire to have Javascript-driven sound for interactive web-based projects. It is free for use and no restrictions apply. It was originally developed for personal use and has since been packaged to perhaps be of use to others.
About
Scott Schiller (contact) is a Front-end Engineer who builds fun creative and technical stuff on the web - or failing that, tries - when he has free time.