Audio Source 2D
Plays a clip or an audio event from its node.
component audio.AudioSource2D
Try it
Start an audio source
The source contains a looping built-in Hum clip, but Play On Start is off. Volume is deliberately 0, so this example makes no audible sound.
Enable Play On Start. After the scene restarts, the real audio service starts a silent voice and the indicator turns green when IsPlaying reports it. Reset stops the voice.
Scene2 nodes
Scriptmain.aes
package demo
// Query the audio service, not the editable PlayOnStart setting. Volume is
// deliberately zero, so this example does not produce audible output.
script VoiceIndicator {
func Update(dt float) {
if node.Get<AudioSource2D>().IsPlaying() {
node.Get<SpriteRenderer>().Tint = Color{0.05, 0.8, 0.25, 1}
} else {
node.Get<SpriteRenderer>().Tint = Color{1, 0.5, 0.05, 1}
}
}
}
Fields
| Name | Type | Default | Description |
|---|---|---|---|
| Clip | Clip | none | Sound to play; ignored when Event is set. |
| Event | AssetRef<data> | none | Audio event to play instead of Clip; its settings replace the fields below. |
| Volume | float | 1 | Volume before the bus: 0 is silent, 1 is the clip as recorded. 0 – 2 |
| Pitch | float | 1 | Playback speed and pitch together: 1 is normal, 2 an octave higher and twice as fast. 0.25 – 4 |
| Pan | float | 0 | Left -1 to right 1, for a sound that is not spatial. -1 – 1 |
| Bus | enum | sfx | Mixer bus. one of: master, music, sfx, ui, ambience, voice |
| Loop | bool | false | Loops until stopped. |
| Play On Start | bool | false | Plays when Play starts. |
| Spatial | bool | false | Fades and pans with the distance to the listener. |
| Min Distance | float | 1 | Full volume up to this distance, in world units. ≥ 0 |
| Max Distance | float | 10 | Silent from this distance, in world units. ≥ 0 |
| Rolloff | Curve | curve | Volume from Min Distance (time 0) to Max Distance (time 1). read-only in scripts |
| Pan Width | float | 0.8 | How far a spatial sound pans when it is at Max Distance to the side: 0 stays centred, 1 is fully left or right. 0 – 1 |
| Pause With | enum | game | game pauses with the world, never keeps playing. one of: game, never |
Runtime state
Values the component has while the game runs. They are not saved with the scene.
| Name | Type | Default | Description |
|---|---|---|---|
| Playing | bool | false | True while a sound of this source plays. |
| Position | float | 0 | Seconds into the sound. |
Methods
IsPlaying method
func (AudioSource2D) IsPlaying() bool
Reports whether the source plays a sound.
Play method
func (AudioSource2D) Play()
Starts the source from the beginning, stopping a sound it already plays.
Stop method
func (AudioSource2D) Stop()
Ends the sound of the source with a short fade.
StopFade method
func (AudioSource2D) StopFade(seconds float)
Fades the sound of the source out over seconds.
Signals
Finished signal
signal Finished(finished bool)
Runs when the sound of the source ends; finished is false after Stop or when it was cut off.
Started signal
signal Started()
Runs when the source starts a sound.
Related guides
- Audio Sound in Aego is imported clips, played by components, scripts and audio events, and mixed on buses that the player sets in Settings.
- Spatial sound A spatial sound fades and pans with its distance to the listener.