> For the complete documentation index, see [llms.txt](https://sonity.gitbook.io/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://sonity.gitbook.io/docs/soundevent/soundevent-functions.md).

# SoundEvent Functions

Reference of all the SoundEvent functions

The [SoundEvent](/docs/soundevent.md) can be played directly from itself instead of playing via the [SoundManager](/docs/soundmanager.md).\
A [SoundManager](/docs/soundmanager.md) instance in the scene is required to play [SoundEvents](/docs/soundevent.md).

Example code:

```csharp
using UnityEngine;
using Sonity;

public class PlayStopExample : MonoBehaviour {

    public SoundEvent soundEvent;

    void PlayExample() {
        // Plays the SoundEvent at the position of the transform
        soundEvent.Play(transform);
    }

    void StopExample() {
        // Stops the SoundEvent playing at the transform
        soundEvent.Stop(transform);
    }
}
```

### Play

{% code fullWidth="false" %}

```csharp
// Plays the SoundEvent at the position of the owner Transform
public void Play(Transform owner);
public void Play(Transform owner, SoundTag localSoundTag);
public void Play(Transform owner, params SoundParameterInternals[] soundParameterInternals);
public void Play(Transform owner, SoundTag localSoundTag, params SoundParameterInternals[] soundParameterInternals);
```

{% endcode %}

<table><thead><tr><th width="218.16668701171875">Parameters</th><th></th></tr></thead><tbody><tr><td>owner</td><td>The owner Transform</td></tr><tr><td>localSoundTag</td><td>The <a href="/docs/soundtag.md">SoundTag</a> which will determine the Local <a href="/docs/soundtag.md">SoundTag</a> of the <a href="/docs/soundevent.md">SoundEvent</a></td></tr><tr><td>soundParameterInternals</td><td>For example <a href="#h.ciw7d4iq7bvo">SoundParameterVolumeDecibel</a> is used to modify how the <a href="/docs/soundevent.md">SoundEvent</a> is played</td></tr></tbody></table>

### PlayAtPosition

```csharp
// Plays the SoundEvent at the Vector3 position with another Transform as the owner
public void PlayAtPosition(Transform owner, Vector3 position);
public void PlayAtPosition(Transform owner, Vector3 position, SoundTag localSoundTag);
public void PlayAtPosition(Transform owner, Vector3 position, params SoundParameterInternals[] soundParameterInternals);
public void PlayAtPosition(Transform owner, Vector3 position, SoundTag localSoundTag, params SoundParameterInternals[] soundParameterInternals);

// Plays the SoundEvent at the Transform position with another Transform as the owner
public void PlayAtPosition(Transform owner, Transform position);
public void PlayAtPosition(Transform owner, Transform position, SoundTag localSoundTag);
public void PlayAtPosition(Transform owner, Transform position, params SoundParameterInternals[] soundParameterInternals);
public void PlayAtPosition(Transform owner, Transform position, SoundTag localSoundTag, params SoundParameterInternals[] soundParameterInternals);
```

<table><thead><tr><th width="212.03338623046875">Parameters</th><th></th></tr></thead><tbody><tr><td>owner</td><td>The owner Transform</td></tr><tr><td>position</td><td>The Transform or Vector3 where is should play at (Transform can follow position)</td></tr><tr><td>localSoundTag</td><td>The <a href="/docs/soundtag.md">SoundTag</a> which will determine the Local <a href="/docs/soundtag.md">SoundTag</a> of the <a href="/docs/soundevent.md">SoundEvent</a></td></tr><tr><td>soundParameterInternals</td><td>For example <a href="#h.ciw7d4iq7bvo">SoundParameterVolumeDecibel</a> is used to modify how the <a href="/docs/soundevent.md">SoundEvent</a> is played</td></tr></tbody></table>

### Stop

{% hint style="info" %}
💡 TIP -  Stopping Loops\
You really only need to use stop for looping or longer oneshot sounds (or the [SoundContainer](/docs/soundcontainer.md) “Stop if Transform is Null” setting).\
Use the polyphony setting in the [SoundEvent](/docs/soundevent.md) to manage the number of instances playing.
{% endhint %}

```csharp
// Stops the SoundEvent at the owner Transform
public void Stop(Transform owner, bool allowFadeOut = true);

// Stops the SoundEvent at the position Transform
public void StopAtPosition(Transform position, bool allowFadeOut = true);

// Stops all the SoundEvents at the owner Transform
public void StopAllAtOwner(Transform owner, bool allowFadeOut = true);

// Stops the SoundEvent everywhere
public void StopEverywhere(bool allowFadeOut = true);

// Stops all SoundEvents
public void StopEverything(bool allowFadeOut = true);

// Stop allowing fade out (Useful for UnityEvents because it only has one parameter)
public void StopAllowFadeOut(Transform owner);
public void StopAtPositionAllowFadeOut(Transform position);
public void StopAllAtOwnerAllowFadeOut(Transform owner);

// Stop without fade out (Useful for UnityEvents because it only has one parameter)
public void StopImmediate(Transform owner);
public void StopAtPositionImmediate(Transform position);
public void StopAllAtOwnerImmediate(Transform owner);
```

<table><thead><tr><th width="134.0166015625">Parameters</th><th></th></tr></thead><tbody><tr><td>owner</td><td>The owner Transform</td></tr><tr><td>position</td><td>The position Transform</td></tr><tr><td>allowFadeOut</td><td>If the <a href="/docs/soundevent.md">SoundEvent</a> should be allowed to fade out. Otherwise it is going to be stopped immediately</td></tr></tbody></table>

### Pause and Unpause

```csharp
// Pauses/unpauses the SoundEvent with the owner Transform locally
public void Pause(Transform owner, bool forcePause = false);
public void Unpause(Transform owner);

// Pauses/unpauses all SoundEvents with the owner Transform locally
public void PauseAllAtOwner(bool forcePause = false);
public void UnpauseAllAtOwner();

// Pauses/unpauses the SoundEvent everywhere locally
public void PauseEverywhere(bool forcePause = false);
public void UnpauseEverywhere();

// Pauses/unpauses the SoundEvent everywhere locally
public void PauseEverything(bool forcePause = false);
public void UnpauseEverything();
```

<table><thead><tr><th width="121.7833251953125">Parameters</th><th></th></tr></thead><tbody><tr><td>owner</td><td>The owner Transform</td></tr><tr><td>forcePause</td><td>If the <a href="/docs/soundevent.md">SoundEvent</a> should be paused even if it is set to "Ignore Local Pause"</td></tr></tbody></table>

### Get State, Length, Time and Contains Loop

{% code overflow="wrap" fullWidth="false" %}

```csharp
// If playing it returns SoundEventState.Playing
// If paused either locally or globally it returns SoundEventState.Paused
// If not playing, but it is delayed it returns SoundEventState.Delayed
// If not playing and it is not delayed it returns SoundEventState.NotPlaying
// If the SoundEvent or Transform is null it returns SoundEventState.NotPlaying
public SoundEventState GetSoundEventState(Transform owner);

// Returns the length (in seconds) of the AudioClip in the last played AudioSource
// Returns Mathf.Infinity if the InstanceSoundEvent is not playing
// Returns Mathf.Infinity if the SoundEvent or Transform is null
// pitchSpeed determines if it should be scaled by pitch. E.g. -12 semitones will be twice as long
public float GetLastPlayedClipLength(Transform owner, bool pitchSpeed);

// Returns the current time (in seconds) of the AudioClip in the last played AudioSource
// Returns 0 if the InstanceSoundEvent is not playing
// Returns 0 if the SoundEvent or Transform is null
// pitchSpeed determines if it should be scaled by pitch. E.g. -12 semitones will be twice as long
public float GetLastPlayedClipTimeSeconds(Transform owner, bool pitchSpeed);

// Returns the current time (in range 0 to 1) of the AudioClip in the last played AudioSource
// Returns 0 if the InstanceSoundEvent is not playing
// Returns 0 if the SoundEvent or Transform is null
public float GetLastPlayedClipTimeRatio(Transform owner);

// Returns the max length (in seconds) of the SoundEvent (calculated from the longest audioClip)
// Is scaled by the pitch of the SoundEvent and SoundContainer
// Does not take into account random, intensity or parameter pitch
public float GetMaxLength();

// Returns the time (in seconds) since the SoundEvent was played
// Is calculated using the time scale selected in the SoundManager
// Returns 0 if the InstanceSoundEvent is not playing
// Returns 0 if the SoundEvent or Transform is null
public float GetTimePlayed(Transform owner);

// Returns if any SoundContainers in the SoundEvent is set to looping
public bool GetContainsLoop();
```

{% endcode %}

<table><thead><tr><th width="132.4833984375">Parameters</th><th></th></tr></thead><tbody><tr><td>owner</td><td>The owner Transform</td></tr><tr><td>pitchSpeed</td><td>Determines if it should be scaled by pitch. E.g. -12 semitones will be twice as long</td></tr></tbody></table>

### Get Spectrum Data

```csharp
// Provides a block of spectrum data from AudioSources
public void GetSpectrumData(Transform owner, ref float[] samples, int channel, FFTWindow window, SpectrumDataFrom spectrumDataFrom);
```

<table><thead><tr><th width="171.183349609375">Parameters</th><th></th></tr></thead><tbody><tr><td>owner</td><td>The owner Transform</td></tr><tr><td>samples</td><td>The array to populate with audio samples. Its length must be a power of 2</td></tr><tr><td>channel</td><td>The channel to sample from</td></tr><tr><td>window</td><td>The FFTWindow type to use when sampling</td></tr><tr><td>spectrumDataFrom</td><td>Where to get the spectrum data from</td></tr></tbody></table>

### Get Last Played AudioSource

```csharp
// Returns the last played AudioSource
// Note that the AudioSource might be stolen or reused for different Voices over time
public AudioSource GetLastPlayedAudioSource(Transform owner);
```

<table><thead><tr><th width="126.75006103515625">Parameters</th><th></th></tr></thead><tbody><tr><td>owner</td><td>The owner Transform</td></tr></tbody></table>

### Load or Unload Audio Data

```csharp
// Loads the audio data of any AudioClips assigned to the SoundContainers of the SoundEvent
public void LoadAudioData();

// Unloads the audio data of any AudioClips assigned to the SoundContainers of the SoundEvent
public void UnloadAudioData();
```

### UI Functions

Useful if you want to play [SoundEvents](/docs/soundevent.md) without passing an owner.

#### **UI Play**

```csharp
// Plays the SoundEvent with the UI Transform as owner
// Useful to play e.g. UI or other 2D sounds without having to pass a Transform
// To make the sound 2D you still need to disable distance and set spatial blend to 0 in the SoundContainer
public void UIPlay();
public void UIPlay(SoundTag localSoundTag);
public void UIPlay(params SoundParameterInternals[] soundParameterInternals);
public void UIPlay(SoundTag localSoundTag, params SoundParameterInternals[] soundParameterInternals);

// Plays the SoundEvent at the position with the UI Transform as owner (Useful for UnityEvents because it only has one parameter)
public void UIPlayAtPosition(Vector3 position);
public void UIPlayAtPosition(Transform position);
```

<table><thead><tr><th width="222.06671142578125">Parameters</th><th></th></tr></thead><tbody><tr><td>localSoundTag</td><td>The <a href="/docs/soundtag.md">SoundTag</a> which will determine the Local <a href="/docs/soundtag.md">SoundTag</a> of the <a href="/docs/soundevent.md">SoundEvent</a></td></tr><tr><td>soundParameterInternals</td><td>For example <a href="/docs/soundparameter.md">SoundParameterVolumeDecibel</a> is used to modify how the <a href="/docs/soundevent.md">SoundEvent</a> is played</td></tr><tr><td>position</td><td>The Transform or Vector3 where is should play at (Transform can follow position)</td></tr></tbody></table>

#### UI Stop

```csharp
// Stops the SoundEvent at the UI Transform
public void UIStop(bool allowFadeOut = true);

// Stops all SoundEvents at the UI Transform
public void UIStopAll(bool allowFadeOut = true);
```

<table><thead><tr><th width="132.48333740234375">Parameters</th><th></th></tr></thead><tbody><tr><td>allowFadeOut</td><td>If the <a href="/docs/soundevent.md">SoundEvent</a> should be allowed to fade out. Otherwise it is going to be stopped immediately</td></tr></tbody></table>

#### UI Pause and Unpause

```csharp
// Pauses/unpauses the SoundEvent at the UI Transform locally
public void UIPause(bool forcePause = false);
public void UIUnpause();

// Pauses/unpauses all SoundEvents at the UI Transform locally
public void UIPauseAll(bool forcePause = false);
public void UIUnpauseAll();
```

<table><thead><tr><th width="135.35003662109375">Parameters</th><th></th></tr></thead><tbody><tr><td>forcePause</td><td>If the <a href="/docs/soundevent.md">SoundEvent</a> should be paused even if it is set to "Ignore Local Pause"</td></tr></tbody></table>

#### UI Get State, Length and Time

```csharp
// Uses the UI owner Transform
// If playing it returns SoundEventState.Playing
// If paused either locally or globally it returns SoundEventState.Paused
// If not playing, but it is delayed it returns SoundEventState.Delayed
// If not playing and it is not delayed it returns SoundEventState.NotPlaying
// If the SoundEvent or Transform is null it returns SoundEventState.NotPlaying
public SoundEventState UIGetSoundEventState();

// Uses the UI owner Transform
// Returns the length (in seconds) of the AudioClip in the last played AudioSource
// Returns Mathf.Infinity if the InstanceSoundEvent is not playing
// Returns Mathf.Infinity if the SoundEvent or Transform is null
// pitchSpeed determines if it should be scaled by pitch. E.g. -12 semitones will be twice as long
public float UIGetLastPlayedClipLength(bool pitchSpeed);

// Uses the UI owner Transform
// Returns the current time (in seconds) of the AudioClip in the last played AudioSource
// Returns 0 if the InstanceSoundEvent is not playing
// Returns 0 if the SoundEvent or Transform is null
// pitchSpeed determines if it should be scaled by pitch. E.g. -12 semitones will be twice as long
public float UIGetLastPlayedClipTimeSeconds(bool pitchSpeed);

// Uses the UI owner Transform
// Returns the current time (in range 0 to 1) of the AudioClip in the last played AudioSource
// Returns 0 if the InstanceSoundEvent is not playing
// Returns 0 if the SoundEvent or Transform is null
public float UIGetLastPlayedClipTimeRatio();

// Uses the UI owner Transform
// Returns the time (in seconds) since the SoundEvent was played
// Is calculated using the time scale selected in the SoundManager
// Returns 0 if the InstanceSoundEvent is not playing
// Returns 0 if the SoundEvent or Transform is null
public float UIGetTimePlayed();

// Returns the owner Transform used by UIPlay() etc
public Transform UIGetTransform();
```

<table><thead><tr><th width="123.16668701171875">Parameters</th><th></th></tr></thead><tbody><tr><td>pitchSpeed</td><td>Determines if it should be scaled by pitch. E.g. -12 semitones will be twice as long</td></tr></tbody></table>

### Music Functions

#### Music Play

Useful if you want to play music with automatic stopping of the old music when playing a new song.

```csharp
// Plays the SoundEvent at the SoundManagers music Transform
public void MusicPlay(bool stopAllOtherMusic = true, bool allowFadeOut = true);
public void MusicPlay(bool stopAllOtherMusic = true, bool allowFadeOut = true, params SoundParameterInternals[] soundParameterInternals);

// Plays allowing fade out (Useful for UnityEvents because it only has one parameter)
public void MusicPlayAllowFadeOut(bool stopAllOtherMusic = true);

// Plays without fade out (Useful for UnityEvents because it only has one parameter)
public void MusicPlayImmediate(bool stopAllOtherMusic = true);
```

<table><thead><tr><th width="220.63336181640625">Parameters</th><th></th></tr></thead><tbody><tr><td>stopAllOtherMusic</td><td>If all other <a href="/docs/soundevent.md">SoundEvents</a> played at the SoundManager music Transform should be stopped</td></tr><tr><td>allowFadeOut</td><td>If the other stopped <a href="/docs/soundevent.md">SoundEvent</a> should be allowed to fade out. Otherwise they are going to be stopped immediately</td></tr><tr><td>soundParameterInternals</td><td>For example <a href="/docs/soundparameter.md">SoundParameterVolumeDecibel</a> is used to modify how the SoundEvent is played</td></tr></tbody></table>

#### Music Stop

```csharp
// Stops the SoundEvent playing at the Music Transform
public void MusicStop(bool allowFadeOut = true);

// Stops all the SoundEvents playing at the Music music Transform
public void AllMusicStop(bool allowFadeOut = true);
```

<table><thead><tr><th width="134.63330078125">Parameters</th><th></th></tr></thead><tbody><tr><td>allowFadeOut</td><td>If the other stopped <a href="/docs/soundevent.md">SoundEvent</a> should be allowed to fade out. Otherwise they are going to be stopped immediately</td></tr></tbody></table>

#### Music Pause and Unpause

```csharp
// Pauses/unpauses the SoundEvent at the Music Transform locally
public void MusicPause(bool forcePause = false);
public void MusicUnpause();

// Pauses/unpauses all SoundEvents at the Music Transform locally
public void AllMusicPause(bool forcePause = false);
public void AllMusicUnpause();
```

<table><thead><tr><th width="123.16668701171875">Parameters</th><th></th></tr></thead><tbody><tr><td>forcePause</td><td>If the <a href="/docs/soundevent.md">SoundEvent</a> should be paused even if it is set to "Ignore Local Pause"</td></tr></tbody></table>

#### Music Get State, Length and Time

```csharp
// Uses the Music owner Transform
// If playing it returns SoundEventState.Playing
// If paused either locally or globally it returns SoundEventState.Paused
// If not playing, but it is delayed it returns SoundEventState.Delayed
// If not playing and it is not delayed it returns SoundEventState.NotPlaying
// If the SoundEvent or Transform is null it returns SoundEventState.NotPlaying
public SoundEventState MusicGetSoundEventState();

// Uses the Music owner Transform
// Returns the length (in seconds) of the AudioClip in the last played AudioSource
// Returns Mathf.Infinity if the InstanceSoundEvent is not playing
// Returns Mathf.Infinity if the SoundEvent or Transform is null
// pitchSpeed determines if it should be scaled by pitch. E.g. -12 semitones will be twice as long
public float MusicGetLastPlayedClipLength(bool pitchSpeed);

// Uses the Music owner Transform
// Returns the current time (in seconds) of the AudioClip in the last played AudioSource
// Returns 0 if the InstanceSoundEvent is not playing
// Returns 0 if the SoundEvent or Transform is null
// pitchSpeed determines if it should be scaled by pitch. E.g. -12 semitones will be twice as long
public float MusicGetLastPlayedClipTimeSeconds(bool pitchSpeed);

// Uses the Music owner Transform
// Returns the current time (in range 0 to 1) of the AudioClip in the last played AudioSource
// Returns 0 if the InstanceSoundEvent is not playing
// Returns 0 if the SoundEvent or Transform is null
public float MusicGetLastPlayedClipTimeRatio();

// Uses the Music owner Transform
// Returns the time (in seconds) since the SoundEvent was played
// Is calculated using the time scale selected in the SoundManager
// Returns 0 if the InstanceSoundEvent is not playing
// Returns 0 if the SoundEvent or Transform is null
public float MusicGetTimePlayed();

// Returns the owner Transform used by PlayMusic() etc
public Transform MusicGetTransform();
```

<table><thead><tr><th width="122.45001220703125">Parameters</th><th></th></tr></thead><tbody><tr><td>pitchSpeed</td><td>Determines if it should be scaled by pitch. E.g. -12 semitones will be twice as long</td></tr></tbody></table>
