SoundComponent

Plays and manages sound.

# Properties

[{"index":"string AudioClipRUID (Sync)","0":"Sets the AudioClipRUID for audio clip playback."}]

[{"index":"boolean Bgm (Sync)","0":"Sets whether to play as background music."}]

[{"index":"float HearingDistance (Sync)","0":"Sets the maximum distance from the listener entity where the sound can be heard."}]

[{"index":"boolean KeepBGM (Sync)","0":"Seamlessly continue playing the audio clip if the previous background music is the same as the current background music. It is applied when the Bgm and PlayOnEnable properties are true and SoundComponent is enabled.Background music does not play seamlessly when it is called using the PlayBGM() function of SoundService."}]

[{"index":"boolean Loop (Sync)","0":"Sets whether to repeat playback."}]

[{"index":"boolean Mute (Sync)","0":"Sets the mute state."}]

[{"index":"float Pitch (Sync)","0":"Sets the pitch and playback speed of the audio clip. The higher the value, the higher the pitch, and the faster the playback speed. Must set a value between 0 and 3."}]

[{"index":"boolean PlayOnEnable (Sync)","0":"Sets whether to play an audio clip with Enable."}]

[{"index":"boolean SetCameraAsListener (Sync)","0":"Adjusts the sound volume according to the distance between the center of the screen and the audio clip. When there is a listener Entity specified through the SetListenerEntity function, the Entity becomes the listener regardless of whether this property is active or not."}]

[{"index":"float Volume (Sync)","0":"Sets the volume. Must set a value between 0 and 1."}]

##### inherited from Component:

[{"index":"boolean Enable (Sync)","0":"Checks whether Component is activated or not."}]

[{"index":"boolean EnableInHierarchy (ReadOnly) (HideFromInspector)","0":"Displays whether this Component is Enable in the hierarchy. If Entity Enable is false regardless of Component Enable, returns false."}]

[{"index":"Entity Entity (ReadOnly)","0":"The Entity that owns this component."}]

# Methods

[{"index":"float GetTimePosition() (ClientOnly)","0":"Returns the current play position of the audio clip in seconds. If the audio clip is not loaded, it logs the error as -1."}]

[{"index":"float GetTotalTime() (ClientOnly)","0":"Returns the total length of the audio clip in seconds. If the audio clip is not loaded, it returns -1 and logs the error."}]

[{"index":"boolean IsAudioClipLoaded() (ClientOnly)","0":"Determines whether the audio clip corresponding to AudioClipRUID is loaded or not. The GetTimePosition(), GetTotalTime(), and SetTimePosition(timeInSecond, targetUserId) functions cannot be executed if no audio clip is loaded."}]

[{"index":"boolean IsPlaying(string targetUserId = nil) (Client)","0":"Checks if music is being played or not."}]

[{"index":"boolean IsSyncedPlaying() (ClientOnly)","0":"Checks whether the synced audio clip is playing. Returns true unless StopSyncedSound has been called separately after PlaySyncedSound was called, or the full duration of the audio clip has been played and stopped."}]

[{"index":"void Pause(string targetUserId = nil) (Client)","0":"Pauses audio clip playback."}]

[{"index":"void Play(string targetUserId = nil) (Client)","0":"Plays the audio clip."}]

[{"index":"void PlaySyncedSound() (Server)","0":"Plays the synchronized audio clip. If BGM property is true, it doesn't work."}]

[{"index":"void Resume(string targetUserId = nil) (Client)","0":"Resumes playing the audio clip."}]

[{"index":"void SetListenerEntity(Entity entity, string targetUserId = nil) (Client)","0":"Adjusts the volume by setting the listener Entity. The greater the distance between the SoundComponent and the listener Entity is, the quieter the sound."}]

[{"index":"void SetTimePosition(float timeInSecond, string targetUserId = nil) (Client)","0":"Sets the current play position to the designated time period. If the audio clip is not loaded, the error is logged."}]

[{"index":"void Stop(string targetUserId = nil) (Client)","0":"Stops playing the audio clip."}]

[{"index":"void StopSyncedSound() (Server)","0":"Stops playing the synchronized audio clip."}]

##### inherited from Component:

[{"index":"boolean IsClient()","0":"Returns whether the current execution environment is a client or not."}]

[{"index":"boolean IsServer()","0":"Returns whether the current execution environment is a server or not."}]

# Events

[{"index":"SoundPlayStateChangedEvent","0":"Event that occurs when the playback state of the background music for the SoundService and sound effect for the SoundComponent change."}]

# Examples

In this example, an audio clip (AudioClipRUID) is set up, and a local player is selected as the listener.  
The audio clip is then configured to become louder the closer the listener gets to the SoundComponent's location.
    
    
    @Component
    script EntitySoundLooper extends Component
        @ExecSpace("ClientOnly")
        method void OnBeginPlay()
        self.Entity.SoundComponent.AudioClipRUID = "000000" 
        self.Entity.SoundComponent.Loop = true
        self.Entity.SoundComponent:SetListenerEntity(_UserService.LocalPlayer)
        self.Entity.SoundComponent:Play()
        end
    end
    

# SeeAlso

  * UserService
  * [Changing Soundtrack](/docs?postId=117)
  * [Creating Sound Effects](/docs?postId=578)