AudioStreamPlayer2D
A sound source with panning and attenuation in a 2D world.
AudioStreamPlayer2D accounts for a listener in its world and viewport. An explicit AudioListener2D defines the listening point; otherwise it uses the center of the viewport's visible region. The viewport must have 2D listening enabled.
MaxDistance uses 2D world units, usually pixels. Panning also depends on the viewport and scale. Playback startup and spatial updates run during physics steps. Collisions alone do not create acoustic occlusion.
Declarations checked against C# SDK 1.0.0. Expand a name to see declarations and documentation.
Constructors1
AudioStreamPlayer2DConstructor
public AudioStreamPlayer2D();Properties14
AreaMaskProperty
public uint AreaMask { get; set; }Determines which Area2D layers affect the sound for reverb and audio bus effects. Areas can be used to redirect AudioStreams so that they play in a certain audio bus. An example of how you might use this is making a "water" area so that sounds played in the water are redirected through an audio bus to make them sound like they are being played underwater.
AttenuationProperty
public float Attenuation { get; set; }The volume is attenuated over distance with this as an exponent.
AutoplayProperty
public bool Autoplay { get; set; }If true, audio plays when added to scene tree.
BusProperty
public StringName Bus { get; set; }Bus on which this audio is playing.
Note: When setting this property, keep in mind that no validation is performed to see if the given name matches an existing bus. This is because audio bus layouts might be loaded after this property is set. If this given name can't be resolved at runtime, it will fall back to "Master".
MaxDistanceProperty
public float MaxDistance { get; set; }Maximum distance from which audio is still hearable.
MaxPolyphonyProperty
public int MaxPolyphony { get; set; }Maximum simultaneous voices in this player. A new Play exceeding the limit replaces the oldest voice. Use a positive value; choose 1 for a single controlled music stream.
PanningStrengthProperty
public float PanningStrength { get; set; }Scales the panning strength for this node by multiplying the base ProjectSettings.audio/general/2d_panning_strength with this factor. Higher values will pan audio from left to right more dramatically than lower values.
PitchScaleProperty
public float PitchScale { get; set; }The pitch and the tempo of the audio, as a multiplier of the audio sample's sample rate.
PlaybackTypeProperty
public AudioServer.PlaybackType PlaybackType { get; set; }The playback type of the stream player. If set other than to the default value, it will force that playback type.
PlayingProperty
public bool Playing { get; set; }If true, audio is playing or is queued to be played (see AudioStreamPlayer2D.Play).
StreamProperty
public AudioStream Stream { get; set; }The AudioStream object to be played.
StreamPausedProperty
public bool StreamPaused { get; set; }Pauses or resumes existing voices. A pending pause is not retained when no voices exist. In Stream mode, pausing makes IsPlaying() false while HasStreamPlayback() remains true.
VolumeDbProperty
public float VolumeDb { get; set; }Base volume before attenuation, in decibels.
VolumeLinearProperty
public float VolumeLinear { get; set; }Base volume before attenuation, as a linear value.
Note: This member modifies AudioStreamPlayer2D.VolumeDb for convenience. The returned value is equivalent to the result of @GlobalScope.db_to_linear on AudioStreamPlayer2D.VolumeDb. Setting this member is equivalent to setting AudioStreamPlayer2D.VolumeDb to the result of @GlobalScope.linear_to_db on a value.
Methods34
GetAreaMaskuint
public uint GetAreaMask();uintGetAttenuationfloat
public float GetAttenuation();floatGetBusStringName
public StringName GetBus();StringNameGetMaxDistancefloat
public float GetMaxDistance();floatGetMaxPolyphonyint
public int GetMaxPolyphony();intGetPanningStrengthfloat
public float GetPanningStrength();floatGetPitchScalefloat
public float GetPitchScale();floatGetPlaybackPositionfloat
public float GetPlaybackPosition();Returns the most recently created voice's position in seconds, not the combined time of all voices. It can advance in steps between mixes. For a single stream at speed 1, time since the last mix can refine an estimate; handle pause, seeking and output latency separately.
floatGetPlaybackTypeAudioServer.PlaybackType
public AudioServer.PlaybackType GetPlaybackType();AudioServer.PlaybackTypeGetStreamAudioStream
public AudioStream GetStream();AudioStreamGetStreamPausedbool
public bool GetStreamPaused();boolGetStreamPlaybackAudioStreamPlayback
public AudioStreamPlayback GetStreamPlayback();Returns the AudioStreamPlayback object associated with this AudioStreamPlayer2D.
AudioStreamPlaybackGetVolumeDbfloat
public float GetVolumeDb();floatGetVolumeLinearfloat
public float GetVolumeLinear();floatHasStreamPlaybackbool
public bool HasStreamPlayback();Checks whether the player retains playback objects, including paused ones. This does not prove audible output: mute, distance or pause can produce silence.
boolIsAutoplayEnabledbool
public bool IsAutoplayEnabled();boolIsPlayingbool
public bool IsPlaying();In Stream mode, returns true for an actively playing voice. Returns false while paused; use HasStreamPlayback() or StreamPaused to detect retained playback. Do not reject a resume command using this method.
boolPlayvoid
public void Play(float fromPosition = 0f);Queues the audio to play on the next physics frame, from the given position from_position, in seconds.
| Name | Type | Default |
|---|---|---|
fromPosition | float | 0f |
Seekvoid
public void Seek(float toPosition);Restarts the player at a position in seconds only while actively playing. Stops previous voices and starts a new one at the position. In Stream mode it does nothing while paused or stopped. To seek while retaining pause, save the flag, call Stop(), Play(position), then restore StreamPaused.
| Name | Type | Default |
|---|---|---|
toPosition | float | — |
SetAreaMaskvoid
public void SetAreaMask(uint mask);| Name | Type | Default |
|---|---|---|
mask | uint | — |
SetAttenuationvoid
public void SetAttenuation(float curve);| Name | Type | Default |
|---|---|---|
curve | float | — |
SetAutoplayvoid
public void SetAutoplay(bool enable);| Name | Type | Default |
|---|---|---|
enable | bool | — |
SetBusvoid
public void SetBus(StringName bus);| Name | Type | Default |
|---|---|---|
bus | StringName | — |
SetMaxDistancevoid
public void SetMaxDistance(float pixels);| Name | Type | Default |
|---|---|---|
pixels | float | — |
SetMaxPolyphonyvoid
public void SetMaxPolyphony(int maxPolyphony);| Name | Type | Default |
|---|---|---|
maxPolyphony | int | — |
SetPanningStrengthvoid
public void SetPanningStrength(float panningStrength);| Name | Type | Default |
|---|---|---|
panningStrength | float | — |
SetPitchScalevoid
public void SetPitchScale(float pitchScale);| Name | Type | Default |
|---|---|---|
pitchScale | float | — |
SetPlaybackTypevoid
public void SetPlaybackType(AudioServer.PlaybackType playbackType);| Name | Type | Default |
|---|---|---|
playbackType | AudioServer.PlaybackType | — |
SetPlayingvoid
public void SetPlaying(bool enable);| Name | Type | Default |
|---|---|---|
enable | bool | — |
SetStreamvoid
public void SetStream(AudioStream stream);| Name | Type | Default |
|---|---|---|
stream | AudioStream | — |
SetStreamPausedvoid
public void SetStreamPaused(bool pause);| Name | Type | Default |
|---|---|---|
pause | bool | — |
SetVolumeDbvoid
public void SetVolumeDb(float volumeDb);| Name | Type | Default |
|---|---|---|
volumeDb | float | — |
SetVolumeLinearvoid
public void SetVolumeLinear(float volumeLinear);| Name | Type | Default |
|---|---|---|
volumeLinear | float | — |
Stopvoid
public void Stop();Stops the audio.
Signals and events1
FinishedEvent
public event Action Finished;Emitted when an update removes one or more naturally ended voices. Other voices may still be playing. Stop() and voice-limit replacement are not natural completion; looping does not emit this signal each cycle.