Create a new Sound Component.
The ComponentSystem that created this component.
The entity that the Component is attached to.
The Entity that this Component is attached to.
The ComponentSystem used to create this Component.
Gets which algorithm to use to reduce the volume of the sound as it moves away from the listener.
Sets which algorithm to use to reduce the volume of the sound as it moves away from the listener. Can be:
Defaults to DISTANCE_LINEAR.
Gets the enabled state of the component.
Sets the enabled state of the component.
Gets the maximum distance from the listener at which audio falloff stops.
Sets the maximum distance from the listener at which audio falloff stops. Note that the volume of the audio is not 0 after this distance, but just doesn't fall off anymore. Defaults to 10000.
Gets the pitch modifier to play the audio with.
Sets the pitch modifier to play the audio with. Must be larger than 0.01. Defaults to 1.
Gets whether the component plays positional sound.
Sets whether the component plays positional sound. If true, the audio will play back at the location of the Entity in space, so the audio will be affected by the position of the AudioListenerComponent. Defaults to true.
Gets the reference distance for reducing volume as the sound source moves further from the listener.
Sets the reference distance for reducing volume as the sound source moves further from the listener. Defaults to 1.
Gets the factor used in the falloff equation.
Sets the factor used in the falloff equation. Defaults to 1.
Gets the volume modifier to play the audio with.
Sets the volume modifier to play the audio with. In range 0-1. Defaults to 1.
Creates a new SoundSlot with the specified name.
The name of the slot.
Optionaloptions: {Settings for the slot.
Optionalasset?: numberThe asset id of the audio asset that is going to be played by this slot.
OptionalautoPlay?: booleanIf true, the slot will start playing as soon as its audio asset is loaded. Defaults to false.
Optionalduration?: numberThe duration of the sound that the slot will play
starting from startTime. Defaults to null which means play to end of the sound.
Optionalloop?: booleanIf true, the sound will restart when it reaches the end. Defaults to false.
Optionaloverlap?: booleanIf 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. Defaults to false.
Optionalpitch?: numberThe relative pitch. Defaults to 1 (plays at normal pitch).
OptionalstartTime?: numberThe start time from which the sound will start playing. Defaults to 0 to start at the beginning.
Optionalvolume?: numberThe playback volume, between 0 and 1. Defaults to 1.
The new slot or null if the slot already exists.
Fire an event, all additional arguments are passed on to the event listener.
Name of event to fire.
Optionalarg1: anyFirst argument that is passed to the event handler.
Optionalarg2: anySecond argument that is passed to the event handler.
Optionalarg3: anyThird argument that is passed to the event handler.
Optionalarg4: anyFourth argument that is passed to the event handler.
Optionalarg5: anyFifth argument that is passed to the event handler.
Optionalarg6: anySixth argument that is passed to the event handler.
Optionalarg7: anySeventh argument that is passed to the event handler.
Optionalarg8: anyEighth argument that is passed to the event handler.
Self for chaining.
Test if there are any handlers bound to an event name.
The name of the event to test.
True if the object has handlers bound to the specified event name.
Returns true if the asset of the slot with the specified name is loaded..
The name of the SoundSlot to look for.
True if the slot with the specified name exists and its asset is loaded.
Returns true if the slot with the specified name is currently paused.
The name of the SoundSlot to look for.
True if the slot with the specified name exists and is currently paused.
Returns true if the slot with the specified name is currently playing.
The name of the SoundSlot to look for.
True if the slot with the specified name exists and is currently playing.
Returns true if the slot with the specified name is currently stopped.
The name of the SoundSlot to look for.
True if the slot with the specified name exists and is currently stopped.
Detach an event handler from an event. If callback is not provided then all callbacks are unbound from the event, if scope is not provided then all events with the callback will be unbound.
Optionalname: stringName of the event to unbind.
Optionalcallback: HandleEventCallbackFunction to be unbound.
Optionalscope: anyScope that was used as the this when the event is fired.
Self for chaining.
const handler = function () {
};
obj.on('test', handler);
obj.off(); // Removes all events
obj.off('test'); // Removes all events called 'test'
obj.off('test', handler); // Removes all handler functions, called 'test'
obj.off('test', handler, this); // Removes all handler functions, called 'test' with scope this
Attach an event handler to an event.
Name of the event to bind the callback to.
Function that is called when event is fired. Note the callback is limited to 8 arguments.
Optionalscope: any = ...Object to use as 'this' when the event is fired, defaults to current this.
Can be used for removing event in the future.
Attach an event handler to an event. This handler will be removed after being fired once.
Name of the event to bind the callback to.
Function that is called when event is fired. Note the callback is limited to 8 arguments.
Optionalscope: any = ...Object to use as 'this' when the event is fired, defaults to current this.
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 SoundComponent#resume.
Optionalname: stringThe name of the slot to pause. Leave undefined to pause everything.
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.
The name of the SoundSlot to play.
The sound instance that will be played. Returns null if the component or its parent entity is disabled or if the SoundComponent has no slot with the specified name.
Removes the SoundSlot with the specified name.
The name of the slot.
Resumes playback of the sound slot with the specified name if it's paused. If no name is specified all slots will be resumed.
Optionalname: stringThe name of the slot to resume. Leave undefined to resume everything.
Returns the slot with the specified name.
The name of the slot.
The slot.
Stops playback of the sound slot with the specified name if it's paused. If no name is specified all slots will be stopped.
Optionalname: stringThe name of the slot to stop. Leave undefined to stop everything.
StaticEVENT_Fired when a sound instance stops playing because it reached its end. The handler is passed the SoundSlot and the SoundInstance that ended.
StaticEVENT_Fired when a sound instance is paused. The handler is passed the SoundSlot and the SoundInstance that was paused.
StaticEVENT_Fired when a sound instance starts playing. The handler is passed the SoundSlot and the SoundInstance that started playing.
StaticEVENT_Fired when a sound instance is resumed. The handler is passed the SoundSlot and the SoundInstance that was resumed.
StaticEVENT_Fired when a sound instance is stopped. The handler is passed the SoundSlot and the SoundInstance that was stopped.
The SoundComponent enables an Entity to play audio. The SoundComponent can manage multiple SoundSlots, each of which can play a different audio asset with its own set of properties such as volume, pitch, and looping behavior.
The SoundComponent supports positional audio, meaning that the sound can be played relative to the Entity's position in 3D space. This is useful for creating immersive audio experiences where the sound's volume and panning are affected by the listener's position and orientation. Positional audio requires that an Entity with an AudioListenerComponent be added to the scene.
You should never need to use the SoundComponent constructor directly. To add a SoundComponent to an Entity, use Entity#addComponent:
Then, to add a sound slot to the component:
Once the SoundComponent is added to the entity, you can set and get any of its properties:
Relevant examples: