SoundEffectPoolComponent


public final class SoundEffectPoolComponent extends Component implements SoundEffectPlayer


Provides positional sound pool audio playback for an Entity.

This component provides a method to play short sounds loaded by a SoundEffectPool. The audio is spatialized based on the Entity's transform and the provided PointSourceParams.

This component can only be attached to one Entity at a time. If the component is detached from an Entity, the audio will become head-locked until re-attached.

Summary

Public methods

static final @NonNull SoundEffectPoolComponent
create(
    @NonNull Session session,
    @NonNull SoundEffectPool soundEffectPool,
    @NonNull PointSourceParams params
)

Creates a SoundEffectPoolComponent.

void
pause(@NonNull Stream stream)

Pauses a currently playing sound stream.

@NonNull Stream
play(
    @NonNull SoundEffect soundEffect,
    @FloatRange(from = 0.0, to = 1.0) float volume,
    @IntRange(from = 0) int priority,
    boolean isLooping
)

Plays a loaded sound effect.

void

Resumes a previously paused sound stream.

void
setLooping(@NonNull Stream stream, boolean isLooping)

Updates the looping status of an active stream.

void
setVolume(
    @NonNull Stream stream,
    @FloatRange(from = 0.0, to = 1.0) float volume
)

Updates the volume of an active stream.

void
stop(@NonNull Stream stream)

Stops a currently playing or paused sound stream and releases that stream ID.

Protected methods

boolean

Called by the framework when this component is being added to an Entity.

void

Called by the framework when this component is being removed from an Entity.

Public methods

create

Added in 1.0.0-alpha15
public static final @NonNull SoundEffectPoolComponent create(
    @NonNull Session session,
    @NonNull SoundEffectPool soundEffectPool,
    @NonNull PointSourceParams params
)

Creates a SoundEffectPoolComponent.

Parameters
@NonNull Session session

the active XR session

@NonNull SoundEffectPool soundEffectPool

pool that manages the loaded sound assets

@NonNull PointSourceParams params

initial spatial audio parameters for this source

pause

public void pause(@NonNull Stream stream)

Pauses a currently playing sound stream.

If the stream has already been stopped or released due to hitting the max stream count then this method has no effect.

Parameters
@NonNull Stream stream

a Stream returned by play

play

public @NonNull Stream play(
    @NonNull SoundEffect soundEffect,
    @FloatRange(from = 0.0, to = 1.0) float volume,
    @IntRange(from = 0) int priority,
    boolean isLooping
)

Plays a loaded sound effect.

The SoundEffects can play concurrently up to the max stream count of the SoundEffectPool from which they were loaded. If the max stream count is exceeded, streams with lower priority are stopped first.

Parameters
@NonNull SoundEffect soundEffect

the handle to the loaded SoundEffect to play

@FloatRange(from = 0.0, to = 1.0) float volume

volume in the range 0.0 to 1.0

@IntRange(from = 0) int priority

playback priority (0 = lowest)

boolean isLooping

true to loop indefinitely, false to play once

Returns
@NonNull Stream

a Stream for controlling this specific instance of the sound

Throws
RuntimeException

if the sound effect fails to play

resume

public void resume(@NonNull Stream stream)

Resumes a previously paused sound stream.

If the stream has already been stopped or released due to hitting the max stream count then this method has no effect.

Parameters
@NonNull Stream stream

a Stream returned by play

setLooping

public void setLooping(@NonNull Stream stream, boolean isLooping)

Updates the looping status of an active stream.

If the stream has already been stopped or released due to hitting the max stream count then this method has no effect.

Parameters
@NonNull Stream stream

a Stream returned by play

boolean isLooping

true to loop indefinitely, false to stop looping

setVolume

public void setVolume(
    @NonNull Stream stream,
    @FloatRange(from = 0.0, to = 1.0) float volume
)

Updates the volume of an active stream.

If the stream has already been stopped or released due to hitting the max stream count then this method has no effect.

Parameters
@NonNull Stream stream

a Stream returned by play

@FloatRange(from = 0.0, to = 1.0) float volume

volume in the range 0.0 to 1.0

stop

public void stop(@NonNull Stream stream)

Stops a currently playing or paused sound stream and releases that stream ID.

If the stream has already been stopped or released due to hitting the max stream count then this method has no effect.

Parameters
@NonNull Stream stream

a Stream returned by play

Protected methods

onAttach

protected boolean onAttach(@NonNull Entity entity)

Called by the framework when this component is being added to an Entity.

This method is triggered when Entity.addComponent is invoked. Implementations should override this method to perform setup logic or to validate if the component is compatible with the provided entity.

Parameters
@NonNull Entity entity

The Entity to which this component is being attached.

Returns
boolean

true if the component was successfully attached; false if the entity does not support this component or if attachment failed.

onDetach

protected void onDetach(@NonNull Entity entity)

Called by the framework when this component is being removed from an Entity.

This method is triggered when Entity.removeComponent is invoked. Implementations should override this method to release resources or undo any changes made during onAttach.

Parameters
@NonNull Entity entity

The Entity from which this component is being detached.