Class: SoundComponent

pc.SoundComponent

The Sound Component controls playback of pc.Sounds.

Constructor

new SoundComponent(system, entity)

Create a new Sound Component.
Parameters:
Name Type Description
system pc.SoundComponentSystem The ComponentSystem that created this Component
entity pc.Entity The entity that the Component is attached to
Properties:
Name Type Description
volume Number The volume modifier to play the audio with. In range 0-1.
pitch Number The pitch modifier to play the audio with. Must be larger than 0.01
positional Boolean If true the audio will play back at the location of the Entity in space, so the audio will be affect by the position of the pc.AudioListenerComponent.
distanceModel String Determines which algorithm to use to reduce the volume of the sound as it moves away from the listener. Can be one of pc.DISTANCE_LINEAR, pc.DISTANCE_INVERSE or pc.DISTANCE_EXPONENTIAL. Default is pc.DISTANCE_LINEAR.
refDistance Number The reference distance for reducing volume as the sound source moves further from the listener.
maxDistance Number The maximum distance from the listener at which audio falloff stops. Note the volume of the audio is not 0 after this distance, but just doesn't fall off anymore.
rollOffFactor Number The factor used in the falloff equation.
slots Object A dictionary that contains the pc.SoundSlots managed by this Component.
Source:

Extends

Methods

addSlot(name, options) → {pc.SoundSlot}

Creates a new pc.SoundSlot with the specified name.
Parameters:
Name Type Description
name String The name of the slot
options Object Settings for the slot
Properties
Name Type Attributes Default Description
volume Number <optional>
1 The playback volume, between 0 and 1.
pitch Number <optional>
1 The relative pitch, default of 1, plays at normal pitch.
loop Boolean <optional>
false If true the sound will restart when it reaches the end.
startTime Number <optional>
0 The start time from which the sound will start playing.
duration Number <optional>
null The duration of the sound that the slot will play starting from startTime.
overlap Boolean <optional>
false If true then sounds played from slot will be played independently of each other. Otherwise the slot will first stop the current sound before starting the new one.
autoPlay Boolean <optional>
false If true the slot will start playing as soon as its audio asset is loaded.
asset Number <optional>
null The asset id of the audio asset that is going to be played by this slot.
Source:
Returns:
The new slot.
Type
pc.SoundSlot
Example
// get an asset by id
var asset = app.assets.get(10);
// add a slot
this.entity.sound.addSlot('beep', {
    asset: asset
});
// play
this.entity.sound.play('beep');

pause(nameopt)

Pauses playback of the slot with the specified name. If the name is undefined then all slots currently played will be paused. The slots can be resumed by calling pc.SoundComponent#resume.
Parameters:
Name Type Attributes Description
name String <optional>
The name of the slot to pause. Leave undefined to pause everything.
Source:
Example
// pause all sounds
this.entity.sound.pause();
// pause a specific sound
this.entity.sound.pause('beep');

play(name) → {pc.SoundInstance}

Begins playing the sound slot with the specified name. The slot will restart playing if it is already playing unless the overlap field is true in which case a new sound will be created and played.
Parameters:
Name Type Description
name String The name of the pc.SoundSlot to play
Source:
Returns:
The sound instance that will be played.
Type
pc.SoundInstance
Example
// get asset by id
var asset = app.assets.get(10);
// create a slot and play it
this.entity.sound.addSlot('beep', {
    asset: asset
});
this.entity.sound.play('beep');

removeSlot(name)

Removes the pc.SoundSlot with the specified name.
Parameters:
Name Type Description
name String The name of the slot
Source:
Example
// remove a slot called 'beep'
this.entity.sound.removeSlot('beep');

resume(name)

Resumes playback of the sound slot with the specified name if it's paused. If no name is specified all slots will be resumed.
Parameters:
Name Type Description
name String The name of the slot to resume. Leave undefined to resume everything.
Source:
Example
// resume all sounds
this.entity.sound.resume();
// resume a specific sound
this.entity.sound.resume('beep');

slot(name) → {pc.SoundSlot}

Returns the slot with the specified name
Parameters:
Name Type Description
name String The name of the slot
Source:
Returns:
The slot
Type
pc.SoundSlot
Example
// get a slot and set its volume
this.entity.sound.slot('beep').volume = 0.5;

stop(name)

Stops playback of the sound slot with the specified name if it's paused. If no name is specified all slots will be stopped.
Parameters:
Name Type Description
name String The name of the slot to stop. Leave undefined to stop everything.
Source:
Example
// stop all sounds
this.entity.sound.stop();
// stop a specific sound
this.entity.sound.stop('beep');

Events

end

Fired when a sound instance stops playing because it reached its ending.
Parameters:
Name Type Description
slot pc.SoundSlot The slot whose instance ended
instance pc.SoundInstance The instance that ended
Source:

pause

Fired when a sound instance is paused.
Parameters:
Name Type Description
slot pc.SoundSlot The slot whose instance was paused
instance pc.SoundInstance The instance that was paused created to play the sound
Source:

play

Fired when a sound instance starts playing
Parameters:
Name Type Description
slot pc.SoundSlot The slot whose instance started playing
instance pc.SoundInstance The instance that started playing
Source:

resume

Fired when a sound instance is resumed..
Parameters:
Name Type Description
slot pc.SoundSlot The slot whose instance was resumed
instance pc.SoundInstance The instance that was resumed
Source:

stop

Fired when a sound instance is stopped.
Parameters:
Name Type Description
slot pc.SoundSlot The slot whose instance was stopped
instance pc.SoundInstance The instance that was stopped
Source: