Aego Русский

Audio Source 2D

Plays a clip or an audio event from its node.

component audio.AudioSource2D
On this page

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.

Starts the scene; open the example parameters to experiment.
Open in AegoDownload example project
Scene2 nodes
Source
Transform
position[0, 0]rotation0scale[1, 1]
Sprite Renderer
spriteTextureasset("ae900000000000000000000000000010")tintColor#f59e0bffsizeVec2[3, 2]
Audio Source 2D
clipClipasset("ae900000000000000000000000000a03")volumefloat0pitchfloat1loopbooltrueplayOnStartboolfalse
Camera
Transform
position[0, 0]rotation0scale[1, 1]
Camera 2D
zoomfloat40clearColorColor#111827ffscaleenumfitVirtualvirtualVec2[960, 540]activebooltrue
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

NameTypeDefaultDescription
ClipClipnone

Sound to play; ignored when Event is set.

EventAssetRef<data>none

Audio event to play instead of Clip; its settings replace the fields below.

Volumefloat1

Volume before the bus: 0 is silent, 1 is the clip as recorded.

0 – 2
Pitchfloat1

Playback speed and pitch together: 1 is normal, 2 an octave higher and twice as fast.

0.25 – 4
Panfloat0

Left -1 to right 1, for a sound that is not spatial.

-1 – 1
Busenumsfx

Mixer bus.

one of: master, music, sfx, ui, ambience, voice
Loopboolfalse

Loops until stopped.

Play On Startboolfalse

Plays when Play starts.

Spatialboolfalse

Fades and pans with the distance to the listener.

Min Distancefloat1

Full volume up to this distance, in world units.

≥ 0
Max Distancefloat10

Silent from this distance, in world units.

≥ 0
RolloffCurvecurve

Volume from Min Distance (time 0) to Max Distance (time 1).

read-only in scripts
Pan Widthfloat0.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 Withenumgame

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.

NameTypeDefaultDescription
Playingboolfalse

True while a sound of this source plays.

Positionfloat0

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.