What is SoundManager?
Choose your "language."
SoundManager 2 is a Javascript Sound API which talks to Flash, effectively mapping most of Flash 8's native sound capabilities to Javascript. It enables web developers and front-end engineers to programmatically control sound in a cross-browser/platform way, using a language they already know.
In short, "It's Javascript Sound for the Web."
(More specifically, it's the Javascript sound API I've always wished the Web had.)
Disclaimer: This is comedy jokes. Sense of humour required.
SoundManagR 2.0 Betatm is a social, long-tail-oriented RIA-based enterprise javascript sound platform which leverages streaming AJAX push technology, Web 2.0, and leveraging. Including plenty of ajaxy goodness, this turn-key, SOA-based and Ajax-enhanced platform will take your Rich Enterprise Applications to the next level, connect the dots and move the needle when mashed up with Web 2.0 collective wisdom of the crowd-wowing features such as drag and drop, auto-complete and real-time performance thanks to enterprise mashup servers.
By leveraging the collective blogosphere and rich folksonomy aspects of the web, it is expected that these supporting technologies may be joining the RIP (Rich Internet Professionals) and DOA (Development Of Asininity) groups within the next few years.*
Woah, cutting-edge semantic web goodness!
"Web 3.0" hasn't been defined yet, but I'm sure it will be really semantic, have new buzzwords and there will be conferences about it.
Of course 2.0 is still cool, but 3.0 is the new ZOMG. SoundManager "Semantic Web Edition" of course is going to leverage the *#@! out of this. How, you ask? Why, with "platforms", "openness", being "open" and "opening up" of course! It'll be a "software-as-an-API-as-a-platform-as-a-service" service-type offering, naturally "cloud"-based to be sure. Perhaps it'll include a widget or two, and maybe a Facebook app to manage your "social graph" (though I hear those are going out of style because nobody likes "walled gardens" any more...)
<jokes />
.. Actually, that's your job, gentle reader: Use this API if it works for you - and buzzword bingo aside, just go and build something cool! :)
SM2 was originally designed as a personal utility, and is freely shared with the hopes of being useful to others. It should be immune to IPOs, Web x.0 nonsense, buzzwords and VCs. ;)
Demos
Less Talk, More Show.
Some examples of Javascript-driven sound, and applications implementing SoundManager 2:
-
"Page as a playlist" | "Page as a playlist" with debug output
Muxtape.com-style UI example, list of playable links with loading/progress/timing display and controls
-
"Make MP3 links playable in-place"
A simple way to play MP3 links "in-place" on a page, easily skinned with CSS.
-
jsAMP MP3 Player Alpha demo | jsAMP with debug output
Experimental MP3 player demo app / API test suite.
-
A drum machine/sampler-style demo.
-
Example illustrating sequencing/timing issues.
-
Simple API call examples.
Getting Started
"I'd like to use this on my site. Where do I start?"
No problem! See this template example which shows the basic requirements of SoundManager 2. (For technical requirements, refer to Requirements + Specifications.)
Like a fine whiskey, SoundManager should come with a disclaimer that reads, "Use Responsibly." You should know when to stop, too.
Licensing
SoundManager 2 is provided free of charge under a BSD license. If you find a nifty or innovative use for it (or just want to comment), feedback is always appreciated.
Download
Current version: 2.5b.20080505
Note: Start-up code has been rewritten since 2.2 to be much faster, somewhat experimental; please report any bugs! :)
SoundManager v2.5b.20080505 (1.44 MB, .zip)
Includes API, ActionScript source, documentation, examples and demos.
Archived beta version (older, onerror()-based startup method): 2.2.20080420 (1.44 MB, .zip)
Basic Use
Adding SoundManager 2 to your page
A single Javascript include will link in all of the required code for the library, which will automatically self-initialise either at document.onDOMContentLoaded() if supported, or alternately, after window.onload() (eg., IE 6 and others.) Once soundManager.go() is called, SM2 will successfully initialise once the Flash movie has loaded and makes a successful callback to Javascript. (Or in Safari's case when you open a tab in the background, "when the tab finally receives focus and the Flash movie loads" - even though window.onload() may have already fired. Fun, eh? ;))
The key point to remember with SM2 is that it will load asynchronously and its onload/onerror handlers may be called either before or after window.onload(), depending on a number of factors.
Including SoundManager 2, within <head>:
<script type="text/javascript" src="soundmanager2.js"></script>
Alternate "minified" version, minus debug code/comments:
<script type="text/javascript" src="soundmanager2-nodebug-jsmin.js"></script>
Once initialised, SM2 will call either soundManager.onload() or soundManager.onerror(), event handlers which you can define just as with window.onload().
For a live example of this code, check the template example.
Successful Initialisation
Once SoundManager has initialised and soundManager.onload() has fired (onload() example), sounds may be created, loaded and played in one call using minimal parameters. Note that this may occur before window.onload() etc., so you may want to wait for that event depending on circumstances.
Example SoundManager calls you can make within soundManager.onload():
soundManager.play('mySound','/path/to/some.mp3');
Sounds can also be created without automatically loading or playing, for later use:
soundManager.createSound('myNewSound','/path/to/some.mp3');
Once defined, sound objects have methods and properties which can be accessed by the soundManager controller.
soundManager.play('myNewSound');
soundManager.setVolume('myNewSound',50);
soundManager.setPan('myNewSound',-100);
Initialisation Failure + Error Handling
In the event SoundManager encounters an error during loading, it will silently fail and disable itself; all subsequent calls to the soundManager controller object will return false. If defined, soundManager.onerror() will be called rather than soundManager.onload(). If enabled, debug messages will be written to the browser and/or console.
Flash Security: Sandbox Types
Flash has a few different sandbox modes; effectively, a Flash movie cannot have access to both the local file system and remote (network/internet) resources simultaneously unless specifically granted access (ie., by whitelisting in the Flash security settings panel.) By default, Flash will be given access to the hosting environment, but not the other. When viewing SM2 demos on your local file system, you may see these warnings as internet URLs may not be loaded by default. When served over HTTP, internet URLs may be loaded but not local filesystem resources by default.
Once initialised, soundManager.sandbox.type will give the Flash-defined sandbox type; for determining restrictions, the following booleans will be defined (initially as null, then false or true once determined): soundManager.sandbox.noRemote and soundManager.sandbox.noLocal. In the event SMSound.onload() fires and the loaded property is false, either a 404 (file not found) or flash security error has likely occurred.
The most common-encountered sandbox types are "remote" (when on the web/HTTP), "localWithFile" and "localTrusted" (when opened from a hard drive directly, and in the latter case, when "whitelisted" under the flash player global security settings panel [see here].)
Debugging + Console Support
During development, SoundManager may throw custom errors to help debugging. For post-live testing, debug mode can be forced by appending debug=1 somewhere in the URL (eg. #debug=1 or ?debug=1) and refreshing the page. In the event pre-onload() errors are preventing debug messages, debug=alert may serve as a last-effort debugging tool. You may also view some live debug output on this page.
Supported debug tool/browser consoles:
- Firefox, via Firebug (console[log|warn|info|error])
- (Theoretically) any UA supporting console.log(string) natively, including Opera + Safari
For those browsers without native console support (or when configured to echo to both console and browser), an element with CSS matching div#soundmanager-debug will be created and appended to the document as soon as possible. Messages will be appended in reverse order in-browser to ease reading, with most recent at the top. You may put this element (an empty DIV with the debug ID) in your page's HTML, and can then apply your own CSS to make it look pretty.
For configuring default debug behaviour, see SoundManager Global Properties.
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.
soundManager.onload = function() {
// soundManager should be ready to use/call at this point
soundManager.createSound('mySound','/path/to/mysoundfile.mp3');
soundManager.play('mySound');
}
SoundManager onerror() Equivalent
If something breaks after Flash attempts to make a call to "JS-land" and vice-versa, soundManager.onerror() should be called if defined. (Note that soundManager.onload() will not be called given the failure.) You can assign a handler here to be notified of failure, and take appropriate action.
soundManager.onerror = function() {
// soundManager failed to initialise - ExternalInterface or other JS error, or missing/old Flash plugin
// Notify user if needed, disable sound-specific functionality etc.
}
SoundManager supported() Method
If you would prefer to call a function to determine the state of SM2 at various times, soundManager.supported() will return a boolean based on 1) An initialisation attempt, and 2) A successful result. This also means that calling supported() prior to SM2's initialisation/onload/onerror process will return false until support has ultimately been determined.
Object Literal Format
Sounds can be created with instance-specific parameters in an object literal (JSON) 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
});
This object can also be passed as an optional argument to the play method, overriding options set at creation time.
For a full list of available options, see Sound Properties Object
SoundManager API
The following are methods, collections, properties and event handlers provided by the globally-scoped soundManager Javascript object. Both sound properties and methods can be set on a global (inherited) default, or per-sound basis.
SoundManager Core Methods
- object:SMSound createSound(object:options)
- Creates a sound with an arbitrary number of optional arguments. Returns a
SMSoundobject instance. -
Example:
soundManager.createSound({ id: 'mySound', // required url: '/audio/mysoundfile.mp3', // required // optional sound parameters here, see Sound Properties for full list volume: 50, autoPlay: true, whileloading: soundIsLoading // remember to omit comma on the last item });Each
createSoundcall results in the creation of aSMSoundobject which stores all properties, methods and events relevant to that particular sound instance.Individual sound objects can also easily be referenced as returned from
createSound:var mySoundObject = soundManager.createSound({ id: 'mySound', url: '/audio/mysoundfile.mp3' }); mySoundObject.play(); // new SMSound object reference, equivalent to referencing soundManager.getSoundById('mySound')(Note: Code formatting is stylistic, not necessarily recommended.) See Object Literal Format.
- createSound(id:string,url:string)
- Creates a sound with the specified ID and URL (simple method.)
- Example:
soundManager.createSound('mySound','/audio/mysoundfile.mp3'); - destroySound(id:string)
- Stops, unloads and destroys a sound specified by ID.
- Example:
soundManager.destroySound('mySound'); - didCreate:boolean play(id:string,[options object])
- Starts playing the sound specified by ID. (Will start loading if applicable, and will play ASAP.)
- Optionally, returns a boolean value indicating "sound created", ie. if the related sound object didn't exist prior to this call and was created to do so.
- Example:
soundManager.play('mySound'); - Note that the second parameter,
options object, is not required and can take almost any argument from the object literal format (eg. volume.) These should be applied on an instance-specific basis (only overriding existing options for this play instance), but may persist due to an incomplete implementation. (To be fixed.) - Example:
soundManager.play('mySound',{volume:50,onfinish:playNextSound}); - setPosition(id:string,msecOffset:integer)
- Seeeks to a given position within a sound, specified by miliseconds (1000 msec = 1 second.)
- Example:
soundManager.setPosition('mySound',2500); - Can only seek within loaded sound data, as defined by the duration property.
- pause(id:string)
- Pauses the sound specified by ID. (Does not toggle.)
- Example:
soundManager.pause('mySound'); - resume(id:string)
- Resumes the currently-paused sound specified by ID.
- Example:
soundManager.resume('mySound'); - togglePause(id:string)
- Pauses/resumes play on the sound specified by ID.
- Example:
soundManager.pause('mySound'); - setVolume(id:string,volume:integer)
- Sets the volume of the sound specified by ID. Accepted values: 0-100
- Example:
soundManager.setVolume('mySound',50); - setPan(id:string,volume:integer)
- Sets the stereo pan (left/right bias) of the sound specified by ID. Accepted values: -100 to 100 (L/R, 0 = center)
- Example:
soundManager.setPan('mySound',-80); - stop(id:string)
- Stops playing the sound specified by ID.
- Example:
soundManager.stop('mySound'); - stopAll()
- Stops any currently-playing sounds.
- Example:
soundManager.stopAll(); - unload(id:string)
- Stops loading the sound specified by ID, canceling any current HTTP request.
- Example:
soundManager.unload('mySound'); - Note that SoundManager does this by loading a new, tiny "stub" MP3 file,
./data/null.mp3, which replaces the current one from loading. This is defined in the SM2 global object asnullURL, and can be edited. - getSoundById(id:string)
- Returns an
SMSoundobject specified by ID. - Example:
var mySMSound = soundManager.getSoundById('mySound'); - loadFromXML(xmlURL:string)
- Loads and creates sounds as defined in a SoundManager v1 XML file (legacy)
- Note that per-sound options are not supported with this method, and sound objects will be created immediately upon loading and parsing of the XML. The sounds will inherit the
defaultOptionssettings, with the exception of thestreamattribute as set in the XML (true if defined, defaultOption applied if omitted.) - Example:
soundManager.loadFromXML('/path/to/some.xml'); - XML format example: MPC drumkit XML file
- supported:boolean supported()
- Returns a boolean indicating whether soundManager has attempted to and succeeded in initialising. This function will return false if called before initialisation has occurred.
- Example:
var isSupported = soundManager.supported();
Sound Properties Object
The soundManager.defaultOptions object contains parameters which are inherited by default when sounds are made via createSound. They can be overridden on a per-sound basis at create time, or changed dynamically in some cases. Note that none of these options are required when calling createSound, as the defaults will be inherited if unspecified.
soundManager.defaultOptions apply the properties and event handlers as specified above. Defaults are shown below as an example.
soundManager.defaultOptions = {
'autoLoad': false, // enable automatic loading (otherwise .load() will be called on demand with .play()..)
'stream': true, // allows playing before entire file has loaded (recommended)
'autoPlay': false, // enable playing of file as soon as possible (much faster if "stream" is true)
'onid3': null, // callback function for "ID3 data is added/available"
'onload': null, // callback function for "load finished"
'whileloading': null, // callback function for "download progress update" (X of Y bytes received)
'onplay': null, // callback for "play" start
'whileplaying': null, // callback during play (position update)
'onstop': null, // callback for "user stop"
'onfinish': null, // callback function for "sound finished playing"
'onbeforefinish': null, // callback for "before sound finished playing (at [time])"
'onbeforefinishtime': 5000, // offset (milliseconds) before end of sound to trigger beforefinish..
'onbeforefinishcomplete': null,// function to call when said sound finishes playing
'onjustbeforefinish': null, // callback for [n] msec before end of current sound
'onjustbeforefinishtime': 200, // [n] - if not using, set to 0 (or null handler) and event will not fire.
'multiShot': true, // let sounds "restart" or layer on top of each other when played multiple times..
'position': null, // offset (milliseconds) to seek to within loaded sound data.
'pan': 0, // "pan" settings, left-to-right, -100 to 100
'volume': 100 // self-explanatory. 0-100, the latter being the max.
}
Note: For live examples, see the code behind the jsAMP MP3 Player demo which uses much of this functionality.
SMSound (Sound Object) Properties
Each createSound() call generates a matching SMSound (sound instance) object, which lasts for the life of the page or until explicitly destroyed. Each instance stores stateful information (eg. playState) and provides event handlers for state changes (eg. onload().)
- sID
- Sound ID string as provided from
createSound()orplay(). - If an ID is known, the related SMSound object can be retrieved via
getSoundByIdor directly referencingsounds[sID]on the SoundManager global object. - url
- The specified URL from which the sound is loaded.
- id3
- An object literal populated, if applicable, when ID3 data is received (related handler:
onid3()) - For property details, see onid3().
- bytesLoaded
- The number of bytes currently received while loading a sound.
- bytesTotal
- The total number of bytes to be downloaded, while loading a sound.
- position
- The current location of the "play head" within the sound, specified in milliseconds (1 sec = 1000 msec).
- duration
- The current length of the sound, specified in milliseconds.
- Note that during loading, this property reflects the length of downloaded data, not the full length, until completely loaded (see whileloading().) For an approximate "full duration" value while loading, see
durationEstimate. - durationEstimate
- The estimated duration of the sound, specified in milliseconds.
- Due to the dynamic nature of
durationwhile loading, this attempts to provide the full duration by calculatingparseInt((self.bytesTotal/self.bytesLoaded)*self.duration)and is updated with eachwhileloading()interval. - Once the sound has fully loaded,
durationshould be referenced as it will contain the final and accurate value. - Note that this method works only with Constant Bitrate (CBR)-encoded MP3s due to the consistent data/time assumption. VBR-encoded MP3s will give inaccurate results.
- loaded
- Boolean value indicating load success as returned from Flash. True indicates success, False is a failure or strangely, a successful load (?) from cache.
- Because of the potential for false positives,
durationand other properties could be checked as a test of whether sound data actually loaded. For more granular state information, see readyState. - playState
- Numeric value indicating the current playing state of the sound.
- 0 = stopped/uninitialised
- 1 = playing or buffering sound (play has been called, waiting for data etc.)
- Note that a 1 may not always guarantee that sound is being heard, given buffering and autoPlay status.
- paused
- Boolean indicating pause status. True/False.
- Treat as read-only; use
pause(),resume()andtogglePause()methods to affect state. - readyState
- Numeric value indicating a sound's current load status
- 0 = uninitialised
- 1 = loading
- 2 = failed/error
- 3 = loaded/success
- didBeforeFinish
- Boolean indicating whether
beforeFinish()condition was reached. - didJustBeforeFinish
- Boolean indicating whether
justBeforeFinish()condition was reached.
SMSound (Sound Object) Events
Not unlike common javascript objects, each SoundManager SMSound (sound instance) object can fire a number of events including onload and others. Functions can be assigned and will be called as needed, and are scoped to the relevant sound object. Specifically, the this keyword will point to the sound object on which the event fired such that its properties can easily be accessed - eg. within an SMSound event handler, this.sID will give the sound ID.
- onload(boolean:success)
- Fires on sound load. Boolean reflects successful load (true), or fail/load from cache (false).
- False value should seemingly only be for failure, but appears to be returned for load from cache as well. This seemingly-strange behaviour comes from Flash. More detail may be available from the Flash 8 sound object documentation.
- Failure can occur if the Flash sandbox (security) model is preventing access, for example loading SoundManager 2 on the local file system and trying to access an MP3 on a network (or internet) URL. (Security can be configured in the Flash security panel, [see here].)
- whileloading()
- Fires at a regular interval when a sound is loading and new data has been received. The relevant, updated property is
bytesLoaded. - Example handler code:
soundManager._writeDebug('sound '+this.sID+' loading, '+this.bytesLoaded+' of '+this.bytesTotal); - Note that the
durationproperty starts from 0 and is updated duringwhileloading()to reflect the duration of currently-loaded sound data (ie. when a 4:00 MP3 has loaded 50%, the duration will be reported as 2:00 in milliseconds.) However, an estimate of final duration can be calculated usingbytesLoaded,bytesTotalanddurationwhile loading. Once fully-loaded,durationwill reflect the true and accurate value. - onplay()
- Fires when
sound.play()is called. - whileplaying()
- Fires at a regular interval when a sound is playing, and a position (time) change is detected. The relevant, updated property is
position. - Example handler code:
soundManager._writeDebug('sound '+this.sID+' playing, '+this.position+' of '+this.duration); - onstop()
- Fires when
sound.stop()is explicitly called. For natural "sound finished"onfinish()case, see below. - onfinish()
- Fires when a playing sound has reached its end. By this point, relevant properties like
playStatewill have been reset to non-playing status. - onbeforefinishcomplete()
- Fires when a sound has finished,
onfinish()has been called, but before the sound play state and other meta data (position, etc.) are reset. - onbeforefinish()
- Fires when a playing, fully-loaded sound has reached
onbeforefinishtime(eg. 5000 msec) from its end. - onjustbeforefinish()
- Fires approximately
justbeforefinishtimebefore the end of a fully-loaded, playing sound. - This is based on a polling approach given SM2 must track the sound's position, and is approximated (eg. a 200 msec value may fire at 187 msec before the end of the sound.)
- onid3()
- Fires when ID3 data has been received. Relevant property is
id3, which is an object literal (JSON)-style object. Only fields with data will be populated. - Note that ID3V2 data is located at the beginning (header) of an MP3 file and will load almost immediately, whereas ID3V1 data is at the end and will not be received until the MP3 has fully loaded.
-
Example handler code:
soundManager._writeDebug('sound '+this.sID+' ID3 data received'); var prop = null; var data = ''; for (prop in this.id3) { data += prop+': '+this.id3[prop]+','; // eg. title: Loser, artist: Beck } - Refer to the Flash 8 Sound.id3 documentation for a list of ID3 properties.
- When parsing ID3 data, it is best to check for the existance of ID3V1 data first, and apply ID3V2 if no matching ID3V1 data is defined. (V1 should "inherit" from V2, ideally, if available.)
- Note that Flash's cross-domain security restrictions may prevent access to ID3 information, even though the MP3 itself can be loaded. (crossdomain.xml files on the remote host can grant Flash permission to access this.)
SMSound (Sound Object) Methods
SoundManager provides wrappers for all SMSound methods - eg. soundManager.play('mySound') checks for a valid sound object, and then calls soundManager.sounds['mySound'].play() on that particular object.
The following methods can be called directly on a SMSound instance. The method calls are the same as the SoundManager global methods documented above for existing sound objects, minus the sound ID parameter.
- play([options object])
- setPosition(msecOffset:integer)
- pause()
- resume()
- togglePause()
- setVolume(volume:integer)
- setPan(pan:integer)
- stop()
- unload()
SoundManager Global Properties
SoundManager includes a few global parameters which configure debug mode, flash movie path and other behaviours.
soundManager.url = '/path/to/soundmanager2.swf';
soundManager.debugMode = true; // enable debugging output (div#soundmanager-debug, OR console..)
soundManager.useConsole = true; // use firebug/safari console.log()-type debug console if available
soundManager.consoleOnly = false; // if console is being used, do not create/write to #soundmanager-debug
soundManager.allowPolling = true; // allow flash to poll for status update..
soundManager.nullURL = '/null.mp3'; // URL of silent/blank MP3 to use when unloading/canceling a loaded/loading sound
To modify global SoundManager defaults, edit the main soundmanager2.js file (look for above section in code) or assign new values in your own application script before window.onload() fires.
Example per-application override:
soundManager.debugMode = false; // disable debug mode
soundManager.defaultOptions.volume = 33; // set global default volume
SoundManager Core Events
The following events are attached to the soundManager global object and are useful for detecting the success/failure of the API's initialisation.
- onload()
- Function called when SoundManager has successfully loaded.
- Example:
soundManager.onload = function() { alert('SoundManager ready to use'); } - Once this function has been called, all core methods will be available to use.
- Note that
onload()is not called when SoundManager fails to load; instead,onerror()is called. - onerror()
- Function called when SoundManager fails to successfully initialise after Flash attempts an init call via ExternalInterface.
- Example:
soundManager.onerror = function() { alert('SoundManager failed to load'); } - This handler should be called if there is an ExternalInterface problem or other exceptions that fire when the initialisation function is called.
SoundManager Object Collections
- soundIDs[]
- An array of sound ID strings, ordered by creation. Can be used to iterate through
sounds{}by ID. - sounds{}
- An object literal/JSON-style instance of
SMSoundobjects indexed by sound ID (as insounds['mySound']orsounds.mySound), used internally by SoundManager.soundManager.getSoundById()may be used as an alternate to directly accessing this object.
Requirements + Specifications
Prerequisites (client)
- Flash plugin, version 8 or higher
- Supported Browser
Supported Browsers/Platforms
Javascript-to-flash communication is possible through Flash 8's ExternalInterface feature, which (as I understand) uses a standard browser plugin architecture implemented by each browser manufacturer (see NPAPI.) As a result, the following browsers should be supported:
- IE 5.0+, Win32
- Netscape 8.0+, Windows/Mac
- Mozilla 1.7.5+, Windows/Mac
- Firefox 1.0+, Windows/Mac
- Firefox 1.5+, Linux (Flash 9 beta)
- Safari 1.3+, Mac
- Opera 9.10+ (slightly buggy, 9.5+ ideal), Windows/Mac
For reference, see Adobe's ExternalInterface support page which documents supported browsers.
At this time, not all combinations of browser/OS have been tested. Some unlisted configurations may be supported, but have not been explicitly verified to work.
Unsupported Configurations
The following browser/OS combinations have been reported as buggy, or may be unsupported:
- Konqueror (version unknown)
Caveats + Limitations / FAQ
Supported sound format (MP3-only, with caveats)
SM2 uses Flash's native Sound object for loading and managing sound, so it is subject to the same limitations that Flash 8 is. Perhaps a design decision, the Flash 8 sound object only supports MP3 files through the loadSound() ActionScript method. SM2 is not able to load other sound formats, including audio-only SWF files, due to this limitation. Refer to the Flash 8 sound object documentation for details.
MP3 Format Caveats
Additionally, some very low and very high bitrate MP3s, and Variable Bitrate (VBR) MP3s may play either too quickly or too slowly (see "the chipmunk problem"); if you are encountering this issue, try re-encoding at a different bitrate (between 64 kbps and 192 kbps, for example.) Using Constant Bitrate (CBR) encoding may also alleviate this problem.
It has been suggested that sample rates that are neither 22/44 KHz can also contribute to this issue.
Looping + multi-shot (overlaying)
Perhaps due to the way Flash dynamically loads and decodes MP3 data, seamless looping doesn't seem to be fully implemented. Loops have a noticeable gap between the finish and start. This has been an issue since the original version of SoundManager. Rather than have a broken feature, the funcionality has been omitted until a solid workaround is found.
Regarding overlaying, even though a multi-shot option can be specified, it does not work in practice; a single instance of a sound can only have one timeline. The current behaviour is that when multiShot is specified and play() is called on a currently-playing sound, it will restart from the beginning without an overlay.
However, the API does provide some creative ways (onbeforefinish for looping, multiple sound objects for multi-shot layering) of working around these Flash limitations.
It should be noted that sounds can loop seamlessly and be layered when linked and exported to SWF from within the Flash IDE, but SoundManager does not support SWF-based audio.
ID3 Parsing
ID3 data can differ in formatting, version and subsequently be oddly-parsed by Flash. Values may sometimes be repeated across different fields.
ID3 info seems to fail to load for iTunes 7-edited files, perhaps due to the format or inclusion of album artwork (image data)
Performance Notes: Caching + RAM Obeservations
Flash appears to use the browser cache (presumably the OS' native, or closest browser,) so the browser's cache size and other settings may affect Flash's cache behaviour. It is safe to assume a 100 MB MP3 will probably not be cached, for example, but a 16 MB one most likely will be.
MP3s appear to be loaded and stored in RAM while loading over HTTP, so memory use needs to be considered for both large MP3s and streaming radio-type applications.
Timing (JS + Flash, ExternalInterface-related)
Javascript-to-Flash communication is not instantaneous on slower systems, but feels much more near real-time on more modern systems. "Lag" can be noted in some cases from function call to sound execution. It is possible some performance analysis can help to speed up this area for timing-critical applications involving animation etc., but this area has not been thoroughly investigated yet. Brad Neuberg has some notes on speeding up ExternalInterface which may be relevant.
Revision History
-
V2.5b.20080505 (current)
The latest and greatest. (Download)
- To improve startup time,
soundManager.go()(createMovie()alias) now fires atdocument.onDOMContentLoaded()by default if supported. (Otherwise, falls back towindow.onload().) - Improved initialisation routine -
soundManager.onerror()is called when the Flash init "times out." Specifically,onerror()is called when Flash fails to make an ExternalInterface (Flash-> JS) call to SM2 within 1 second ofwindow.onload()firing. - Added logic to handle special Safari delayed init case (Flash not loading when in a new, unfocused tab until focused) as a exception to the above.
- Added better exception handling + debug messaging for initialisation failure cases (Flash security restrictions due to loading from local file system, no flash support, no ExternalInterface support etc.)
- Updated .swf appendChild() target to use best-to-worst options:
(document.body || document.documentElement || document.getElementsByTagName('div')[0]) - Safari console[log|warn|error]-style messages are now properly formatted.
- Added tons of semicolons to closing braces, eg.
}; - "No-debug", minified version of SM2 included: soundmanager2-nodebug-jsmin.js (17.4 KB, down from full size of 35 KB.) With Gzip compression, file size is ~6 KB. (Commented, debug-enabled version compresses to 10 KB with Gzip.)
- To improve startup time,
-
V2.5b.20080501
Warning: A little experimental too, read details below.
Changelog:
- Rewrote SoundManager initialisation: "Way faster." Communication now initiated from Flash, verification callback then performed by JS; far faster, hopefully more-reliable (TBD.) Init time drastically reduced from seconds to milliseconds in most cases, dependent primarily on Flash movie load rather than
window.onload(). - Above change also fixes Safari "loading SM2 in background tab" issue, where Safari does not init Flash until background tab comes into focus (when a link is opened in a new, non-focused tab.)
- Current drawback: Difficult to determine, save for falling back to
window.onload()plus focus methods due to above issue, whether SM2 is actually available or not (ie.,soundManager.onerror()will not likely be called as in past.) However, thesupported()method will correctly reflect if SM2 has successfully initialised, for example. - Added sandbox/security model code; SM2 can now tell if it is restricted to either local or internet access only, for example. Helpful in potential debugging errors, plus viewing demos off the local filesystem should no longer throw init errors requiring whitelisting (seemingly due to the new initialisation method.) Win!
- Opera 9.27 has been noted to have some bugs relating to ExternalInterface, seems to be unable to make calls from ActionScript-level methods using
setTimeout()orsetInterval(). As a reulst, SoundManager 2 events likeonfinish(),whileplaying()andonfinish()can be sporadically called or missed altogether. No known workaround at this time, but Opera 9.5 (beta 2) does not have this issue. Popular MP3 "mix tape" site muxtape.com uses similar techniques for JS-Flash communication and appears to suffer from the same problem. - Warning: Random crash issue noticed when using IE 6 + 7 and this demo page, calling
createSound()whensoundManager.defaultOptions.autoLoad = true;from withinsoundManager.onload(), for creating + preloading the tab/theme switch sounds. Removing autoLoad=true (leaving the default of false) fixed the crash. Exact reason not determined, perhaps recursive calls or pre-onload issue (?), seems to be isolated to the home page. MPC demo uses autoLoad also, but did not crash. Mentioning just in case. - Updated Muxtape-style demo: More themes, load/security debugging info etc.
- Rewrote SoundManager initialisation: "Way faster." Communication now initiated from Flash, verification callback then performed by JS; far faster, hopefully more-reliable (TBD.) Init time drastically reduced from seconds to milliseconds in most cases, dependent primarily on Flash movie load rather than
-
V2.2.20080420
Changelog:
- More demos! "Page as a playlist" (muxtape.com-style) example, "Make MP3 links playable inline" demo
- Corrected
onStop()handler inheritance/overriding behaviour (was incorrectly checking defaultOptions) - Added debug output of options object for
createSound()calls. Full options (result of merging global default + sound-instance-specific options) displayed, helpful in troubleshooting. Event handler function code is intelligently (hopefully) displayed, truncated at 64 characters of first block or end of line, whichever comes first. - Removed most HTML markup from non-HTML (eg. console)
_writeDebug()calls soundManager.destruct()writes to console, to be consistent
V2.1.20080331
Changelog:
- Modified
createSound()to return a sound object if successful (more logical) - Updated
setPosition()method and addedpositionoption parameter, documentation + demo (bugfix) - Corrected
createSound()andplay()sound option inheritance/overriding behaviour (eg.position) to work as expected (most to least important: Method call options -> sound object instance options -> SM2 global options) - Updated
deleteSound()so Array.splice() is used instead of delete, the latter doesn't cause Array.length to update (bugfix) - Modified debug=alert to only work when debug mode is enabled (potential annoyance aversion)
- Modified
togglePause()to usepositionoption parameter rather than undocumentedoffset(oops :D) - Added
supported()convenience method (indicates pass/fail after SM2 has initialised.) - Added disabling debug calls from Flash (performance)
- Added URL hash updating/bookmarking and page title updating to jsAMP demo app
- Updated project page layout
V2.0b.20070415
Changelog:
- Added
destroySound()method - Made debug output slightly less-verbose (commented out)
- Safety tweak for position-related Flash bug when loading new sounds
- Highly-expanded documentation (
SMSoundevents + properties, examples, caveats, FAQs etc.) - Added time-sensitive light/dark theme for documentation
V2.0b.20070201
Second beta?
Changelog:
- Fixed stopAll() bug (previously broken)
- Added
nullURLparameter - Updated documentation
V2.0b.20070123
V2.0b.20070118
V2.0b.20070115
-
V2.0b.20070107
First beta
-
V2.0a.20060904
Prerelease alpha
"Use Responsibly"
A Word Of Vice
Not every button, link, element or paragraph on the web needs to zoom, move, change colour and be noisy, repetitive and annoying all at the same time. Use your own discretion! :)
Demo Credits
Background Tile
Modified from squidfingers.com (free patterns)
Sound Sources
MPC Demo Sounds downloaded from AKAI.com
Theme switch SFX from user pempi, freesoundproject (Creative Commons Sampling Plus 1.0 License)
Tab switch SFX from user TicTacShutUp, freesoundproject (Creative Commons Sampling Plus 1.0 License)
General
SoundManager was written to meet a desire to have Javascript-driven sound for interactive web-based projects. It is free for use in both personal and commercial projects (see Licensing.) It was originally developed for personal use and has been packaged with the hopes of being useful to others.
Related Projects
JavaScript/DHTML/Flash MP3 players
If you're searching for an included/embedded-type media player, these related projects might of use.
- Yahoo! Media Player supports a number of different formats depending on installed plugins (WMA etc.), can display artist / album information and so on.
- JSSoundkit, an MP3 player with a simple UI.
Links
- SoundManager 2 Project Page (schillmania.com)
- Flash 8 sound object documentation (livedocs.macromedia.com)
- Flash Player Global Security Settings Panel - Configure your Flash Player security, eg., allow SM2 .swf on your local filesystem to have internet access for testing by whitelisting its location eg. file://Users/Administrator/Desktop/soundmanager... etc. Alternately, view the page over HTTP and access internet resources that way.
About
Scott Schiller (contact) is a Front-end Engineer (previously "Web Developer") who builds fun creative and technical stuff on the web - or failing that, tries - when he has free time. He likes building cool things which contribute to, yet enjoys mocking, the Web 2.0 meme. (See How Web 2.0-Aware Are You?)
Debug Output
Live debug info from SoundManager 2, active on this page for demo tests.
If you're seeing errors here or in demos, this info can help in troubleshooting.