SoundJS v1.0.0 API Documentation : AbstractSoundInstance (original) (raw)

A AbstractSoundInstance is created when any calls to the Sound API method play orcreateInstance are made. The AbstractSoundInstance is returned by the active plugin for control by the user.

Example

 var myInstance = createjs.Sound.play("myAssetPath/mySrcFile.mp3");

A number of additional parameters provide a quick way to determine how a sound is played. Please see the Sound API method play for a list of arguments.

Once a AbstractSoundInstance is created, a reference can be stored that can be used to control the audio directly through the AbstractSoundInstance. If the reference is not stored, the AbstractSoundInstance will play out its audio (and any loops), and is then de-referenced from the Sound class so that it can be cleaned up. If audio playback has completed, a simple call to the play instance method will rebuild the references the Sound class need to control it.

 var myInstance = createjs.Sound.play("myAssetPath/mySrcFile.mp3", {loop:2});
 myInstance.on("loop", handleLoop);
 function handleLoop(event) {
     myInstance.volume = myInstance.volume * 0.5;
 }

Events are dispatched from the instance to notify when the sound has completed, looped, or when playback fails

 var myInstance = createjs.Sound.play("myAssetPath/mySrcFile.mp3");
 myInstance.on("complete", handleComplete);
 myInstance.on("loop", handleLoop);
 myInstance.on("failed", handleFailed);

Constructor

AbstractSoundInstance

(

)

Parameters:

Item Index

Methods

Properties

Methods

_addLooping

protected

Internal function called when looping is added during playback.

Parameters:

_beginPlaying

Boolean protected

Called by the Sound class when the audio is ready to play (delay has completed). Starts sound playing if the src is loaded, otherwise playback will fail.

Returns:

_calculateCurrentPosition

() protected

Internal function that calculates the current position of the playhead and sets this._position to that value

_cleanUp

() protected

Clean up the instance. Remove references and clean up any additional properties such as timers.

_getDuration

() Number protected

Please use duration directly as a property

Returns:

Number:

The duration of the sound instance in milliseconds.

_getLoop

() Number protected

Please use loop directly as a property

Returns:

_getMuted

() Boolean protected

Please use muted directly as a property

Returns:

_getPan

() Number protected

Please use pan directly as a property

Returns:

Number:

The value of the pan, between -1 (left) and 1 (right).

_getPaused

() Boolean protected

Please use paused directly as a property.

Returns:

Boolean:

If the instance is currently paused

_getPlaybackResource

Object protected

Parameters:

Returns:

Object:

playback resource used for playing audio

_getPosition

() Number protected

Please use position directly as a property

Returns:

Number:

The position of the playhead in the sound, in milliseconds.

_getStartTime

() Number protected

Please use startTime directly as a property

Returns:

Number:

The startTime of the sound instance in milliseconds.

_getVolume

() Number protected

Please use volume directly as a property

Returns:

Number:

The current volume of the sound instance.

_handleCleanUp

() protected

Internal function called when AbstractSoundInstance is being cleaned up

_handleLoop

() protected

Internal function called when AbstractSoundInstance has played to end and is looping

_handleSoundComplete

protected

Audio has finished playing. Manually loop it if required.

_handleSoundReady

() protected

Handles starting playback when the sound is ready for playing.

_handleStop

() protected

Internal function called when stopping playback

_interrupt

() protected

The sound has been interrupted.

_pause

() protected

Internal function called when pausing playback

_playFailed

() private

Play has failed, which can happen for a variety of reasons. Cleans up instance and dispatches failed event

_removeLooping

protected

Internal function called when looping is removed during playback.

Parameters:

_resume

() protected

Internal function called when resuming playback

_sendEvent

protected

A helper method that dispatches all events for AbstractSoundInstance.

Parameters:

_setLoop

protected

Please use loop directly as a property

Parameters:

_setPan

AbstractSoundInstance protected

Please use pan directly as a property

Parameters:

Returns:

_updateDuration

() protected

Internal function used to update the duration of the audio.

_updateDuration

() protected

Internal function used to get the duration of the audio from the source we'll be playing.

_updatePan

() protected

Internal function used to update the pan

_updatePosition

() protected

Internal function used to update the position of the playhead.

_updateStartTime

() protected

Internal function used to update the startTime of the audio.

_updateVolume

() protected

Internal function used to update the volume based on the instance volume, master volume, instance mute value, and master mute value.

addEventListener

(

)

Function | Object

Adds the specified event listener. Note that adding multiple listeners to the same function will result in multiple callbacks getting fired.

Example

 displayObject.addEventListener("click", handleClick);
 function handleClick(event) {
    // Click happened.
 }

Parameters:

Returns:

applyPlayProps

AbstractSoundInstance

Takes an PlayPropsConfig or Object with the same properties and sets them on this instance.

Parameters:

Returns:

destroy

()

Remove all external references and resources from AbstractSoundInstance. Note this is irreversible and AbstractSoundInstance will no longer work

dispatchEvent

(

)

Boolean

Dispatches the specified event to all listeners.

Example

 // Use a string event
 this.dispatchEvent("complete");

 // Use an Event instance
 var event = new createjs.Event("progress");
 this.dispatchEvent(event);

Parameters:

Returns:

Boolean:

Returns false if preventDefault() was called on a cancelable event, true otherwise.

hasEventListener

Boolean

Indicates whether there is at least one listener for the specified event type.

Parameters:

Returns:

Boolean:

Returns true if there is at least one listener for the specified event.

off

(

)

A shortcut to the removeEventListener method, with the same parameters and return value. This is a companion to the .on method.

IMPORTANT: To remove a listener added with on, you must pass in the returned wrapper function as the listener. See on for an example.

Parameters:

on

(

)

Function

A shortcut method for using addEventListener that makes it easier to specify an execution scope, have a listener only run once, associate arbitrary data with the listener, and remove the listener.

This method works by creating an anonymous wrapper function and subscribing it with addEventListener. The wrapper function is returned for use with removeEventListener (or off).

IMPORTANT: To remove a listener added with on, you must pass in the returned wrapper function as the listener, or useremove. Likewise, each time you call on a NEW wrapper function is subscribed, so multiple calls to on with the same params will create multiple listeners.

Example

    var listener = myBtn.on("click", handleClick, null, false, {count:3});
    function handleClick(evt, data) {
        data.count -= 1;
        console.log(this == myBtn); // true - scope defaults to the dispatcher
        if (data.count == 0) {
            alert("clicked 3 times!");
            myBtn.off("click", listener);
            // alternately: evt.remove();
        }
    }

Parameters:

Returns:

Function:

Returns the anonymous function that was created and assigned as the listener. This is needed to remove the listener later using .removeEventListener.

play

AbstractSoundInstance

Play an instance. This method is intended to be called on SoundInstances that already exist (created with the Sound API createInstance or play).

Example

 var myInstance = createjs.Sound.createInstance(mySrc);
 myInstance.play({interrupt:createjs.Sound.INTERRUPT_ANY, loop:2, pan:0.5});

Note that if this sound is already playing, this call will still set the passed in parameters.

Parameters Deprecated
The parameters for this method are deprecated in favor of a single parameter that is an Object or PlayPropsConfig.

Parameters:

Returns:

removeAllEventListeners

Removes all listeners for the specified type, or all listeners of all types.

Example

 // Remove all listeners
 displayObject.removeAllEventListeners();

 // Remove all click listeners
 displayObject.removeAllEventListeners("click");

Parameters:

removeEventListener

(

)

Removes the specified event listener.

Important Note: that you must pass the exact function reference used when the event was added. If a proxy function, or function closure is used as the callback, the proxy/closure reference must be used - a new proxy or closure will not work.

Example

 displayObject.removeEventListener("click", handleClick);

Parameters:

stop

() AbstractSoundInstance

Stop playback of the instance. Stopped sounds will reset their position to 0, and calls to AbstractSoundInstance/resume will fail. To start playback again, call play.

If you don't want to lose your position use yourSoundInstance.paused = true instead. AbstractSoundInstance/paused.

Example

myInstance.stop();

Returns:

toString

() String

Returns:

String:

a string representation of the instance.

willTrigger

Boolean

Indicates whether there is at least one listener for the specified event type on this object or any of its ancestors (parent, parent's parent, etc). A return value of true indicates that if a bubbling event of the specified type is dispatched from this object, it will trigger at least one listener.

This is similar to hasEventListener, but it searches the entire event flow for a listener, not just this object.

Parameters:

Returns:

Boolean:

Returns true if there is at least one listener for the specified event.

Properties

_captureListeners

Object protected

delayTimeoutId

TimeoutVariable protected

A Timeout created by Sound when this AbstractSoundInstance is played with a delay. This allows AbstractSoundInstance to remove the delay if stop, pause, or cleanup are called before playback begins.

Default: null

duration

Number

Sets or gets the length of the audio clip, value is in milliseconds.

Default: 0

loop

Number public

The number of play loops remaining. Negative values will loop infinitely.

Default: 0

muted

Boolean

Mutes or unmutes the current audio instance.

Default: false

pan

Number

The pan of the sound, between -1 (left) and 1 (right). Note that pan is not supported by HTML Audio.

Note in WebAudioPlugin this only gives us the "x" value of what is actually 3D audio

Default: 0

paused

Boolean

Pauses or resumes the current audio instance.

playbackResource

Object

Object that holds plugin specific resource need for audio playback. This is set internally by the plugin. For example, WebAudioPlugin will set an array buffer, HTMLAudioPlugin will set a tag, FlashAudioPlugin will set a flash reference.

Default: null

playState

String

The play state of the sound. Play states are defined as constants on Sound.

Default: null

position

Number

The position of the playhead in milliseconds. This can be set while a sound is playing, paused, or stopped.

Default: 0

src

String

The source of the sound.

Default: null

startTime

Number

Audio sprite property used to determine the starting offset.

Default: 0

uniqueId

String | Number

The unique ID of the instance. This is set by Sound.

Default: -1

volume

Number

The volume of the sound, between 0 and 1.

The actual output volume of a sound can be calculated using:myInstance.volume * createjs.Sound._getVolume();

Default: 1

Events

complete

The event that is fired when playback completes. This means that the sound has finished playing in its entirety, including its loop iterations.

Event Payload:

failed

The event that is fired when playback has failed. This happens when there are too many channels with the same src property already playing (and the interrupt value doesn't cause an interrupt of another instance), or the sound could not be played, perhaps due to a 404 error.

Event Payload:

interrupted

The event that is fired when playback is interrupted. This happens when another sound with the same src property is played using an interrupt value that causes this instance to stop playing.

Event Payload:

loop

The event that is fired when a sound has completed playing but has loops remaining.

Event Payload:

succeeded

The event that is fired when playback has started successfully.

Event Payload: