WebAudioPlugin Class
Play sounds using Web Audio in the browser. The WebAudioPlugin is currently the default plugin, and will be used anywhere that it is supported. To change plugin priority, check out the Sound API registerPlugins method.
Known Browser and OS issues for Web Audio
Firefox 25- mp3 audio files do not load properly on all windows machines, reported here. For this reason it is recommended to pass another FF supported type (ie ogg) first until this bug is resolved, if possible.
Webkit (Chrome and Safari)
- AudioNode.disconnect does not always seem to work. This can cause the file size to grow over time if you are playing a lot of audio files.
iOS 6 limitations
- Sound is initially muted and will only unmute through play being called inside a user initiated event (touch/click).
- A bug exists that will distort uncached audio when a video element is present in the DOM. You can avoid this bug by ensuring the audio and video audio share the same sampleRate.
Constructor
WebAudioPlugin
()
Item Index
Methods
- _addPropsToClasses static
- _compatibilitySetUp static
- _generateCapabilities static
- _handlePreloadComplete
- _handlePreloadError
- _isFileXHRSupported static
- _updateVolume
- create
- getVolume
- initialize deprecated
- isPreloadComplete
- isPreloadStarted
- isSupported static
- playEmptySound static
- preload
- register
- removeAllSounds
- removeSound
- setMute
- setVolume
Properties
- _audioSources
- _capabilities static
- _panningModel
- _panningModel static
- _volume
- context
- context static
- dynamicsCompressorNode
- gainNode
Methods
_addPropsToClasses
()
protected
static
Set up needed properties on supported classes WebAudioSoundInstance and WebAudioLoader.
_compatibilitySetUp
()
protected
static
Set up compatibility if only deprecated web audio calls are supported. See http://www.w3.org/TR/webaudio/#DeprecationNotes Needed so we can support new browsers that don't support deprecated calls (Firefox) as well as old browsers that don't support new calls.
_generateCapabilities
()
protected
static
Determine the capabilities of the plugin. Used internally. Please see the Sound API getCapabilities method for an overview of plugin capabilities.
_handlePreloadComplete
()
protected
Handles internal preload completion.
_isFileXHRSupported
()
Boolean
protected
static
Determine if XHR is supported, which is necessary for web audio.
Returns:
If XHR is supported.
_updateVolume
()
protected
Set the gain value for master audio. Should not be called externally.
create
-
src
-
startTime
-
duration
Create a sound instance. If the sound has not been preloaded, it is internally preloaded here.
Parameters:
Returns:
A sound instance for playback and control.
getVolume
()
Number
Get the master volume of the plugin, which affects all SoundInstances.
Returns:
The volume level, between 0 and 1.
initialize
()
deprecated
protected
isPreloadComplete
-
src
Checks if preloading has finished for a specific source.
Parameters:
-
src
StringThe sound URI to load.
Returns:
isPreloadStarted
-
src
Checks if preloading has started for a specific source. If the source is found, we can assume it is loading, or has already finished loading.
Parameters:
-
src
StringThe sound URI to check.
Returns:
isSupported
()
Boolean
static
Determine if the plugin can be used in the current browser/OS.
Returns:
If the plugin can be initialized.
playEmptySound
()
static
Plays an empty sound in the web audio context. This is used to enable web audio on iOS devices, as they require the first sound to be played inside of a user initiated event (touch/click). This is called when WebAudioPlugin is initialized (by Sound initializeDefaultPlugins for example).
Example
function handleTouch(event) {
createjs.WebAudioPlugin.playEmptySound();
}
preload
-
loader
Internally preload a sound.
Parameters:
-
loader
LoaderThe sound URI to load.
register
-
loadItem
Pre-register a sound for preloading and setup. This is called by Sound.
Note all plugins provide a Loader
instance, which PreloadJS
can use to assist with preloading.
Parameters:
-
loadItem
StringAn Object containing the source of the audio Note that not every plugin will manage this value.
Returns:
A result object, containing a "tag" for preloading purposes.
removeAllSounds
-
src
Remove all sounds added using WebAudioPlugin/register. Note this does not cancel a preload.
Parameters:
-
src
StringThe sound URI to unload.
removeSound
-
src
Remove a sound added using WebAudioPlugin/register. Note this does not cancel a preload.
Parameters:
-
src
StringThe sound URI to unload.
Properties
_audioSources
Object
protected
Object hash indexed by the source URI of each file to indicate if an audio source has begun loading, is currently loading, or has completed loading. Can be used to store non boolean data after loading is complete (for example arrayBuffers for web audio).
_capabilities
Object
protected
static
The capabilities of the plugin. This is generated via the _generateCapabilities method and is used internally.
Default: null
_panningModel
Number / String
protected
Value to set panning model to equal power for WebAudioSoundInstance. Can be "equalpower" or 0 depending on browser implementation.
_panningModel
Number / String
protected
static
Value to set panning model to equal power for WebAudioSoundInstance. Can be "equalpower" or 0 depending on browser implementation.
context
AudioContext
The web audio context, which WebAudio uses to play audio. All nodes that interact with the WebAudioPlugin need to be created within this context.
context
AudioContext
static
The web audio context, which WebAudio uses to play audio. All nodes that interact with the WebAudioPlugin need to be created within this context.
Advanced users can set this to an existing context, but must do so before they call registerPlugins or initializeDefaultPlugins.
dynamicsCompressorNode
AudioNode
A DynamicsCompressorNode, which is used to improve sound quality and prevent audio distortion.
It is connected to context.destination
.
Can be accessed by advanced users through createjs.Sound.activePlugin.dynamicsCompressorNode.
gainNode
AudioGainNode
A GainNode for controlling master volume. It is connected to dynamicsCompressorNode.
Can be accessed by advanced users through createjs.Sound.activePlugin.gainNode.