AudioStreamPlayer
Description
Section titled “Description”A node for audio playback.
The AudioStreamPlayer node plays an audio stream non-positionally. It is ideal for user interfaces, menus, or background music.
To use this node, stream needs to be set to a valid AudioStream resource. Playing more than one sound at the same time is also supported, see max_polyphony.
If you need to play audio at a specific position, use AudioStreamPlayer2D or AudioStreamPlayer3D instead.
Properties
Section titled “Properties”| Type | Name | Default |
|---|---|---|
| bool | autoplay |
false |
| StringName | bus |
&"Master" |
| int | max_polyphony |
1 |
| MixTarget | mix_target |
0 |
| float | pitch_scale |
1.0 |
| AudioSystem.PlaybackType | playback_type |
0 |
| bool | playing |
false |
| AudioStream | stream |
|
| bool | stream_paused |
false |
| float | volume_db |
0.0 |
| float | volume_linear |
Methods
Section titled “Methods”| Returns | Method |
|---|---|
| float | get_playback_position() |
| AudioStreamPlayback | get_stream_playback() |
| bool | has_stream_playback() |
| void | play(from_position: float = 0.0) |
| void | seek(to_position: float) |
| void | stop() |
Signals
Section titled “Signals”finished
finished()
Emitted when a sound finishes playing without interruptions. This signal is not emitted when calling stop(), or when exiting the tree while sounds are playing.
Enumerations
Section titled “Enumerations”MixTarget
enum MixTarget
MIX_TARGET_STEREO = 0
The audio will be played only on the first channel. This is the default.
MIX_TARGET_SURROUND = 1
The audio will be played on all surround channels.
MIX_TARGET_CENTER = 2
The audio will be played on the second channel, which is usually the center.
Property descriptions
Section titled “Property descriptions”autoplay
bool autoplay = false
If true, this node calls play() when entering the tree.
bus
StringName bus = &"Master"
The target bus name. All sounds from this node will be playing on this bus.
Note: At runtime, if no bus with the given name exists, all sounds will fall back on "Master". See also AudioSystem.get_bus_name().
max_polyphony
int max_polyphony = 1
The maximum number of sounds this node can play at the same time. Calling play() after this value is reached will cut off the oldest sounds.
mix_target
MixTarget mix_target = 0
The mix target channels. Has no effect when two speakers or less are detected (see AudioSystem.SpeakerMode).
pitch_scale
float pitch_scale = 1.0
The audio’s pitch and tempo, as a multiplier of the stream’s sample rate. A value of 2.0 doubles the audio’s pitch, while a value of 0.5 halves the pitch.
playback_type
AudioSystem.PlaybackType playback_type = 0
The playback type of the stream player. If set other than to the default value, it will force that playback type.
playing
bool playing = false
If true, this node is playing sounds. Setting this property has the same effect as play() and stop().
stream
AudioStream stream
The AudioStream resource to be played. Setting this property stops all currently playing sounds. If left empty, the AudioStreamPlayer does not work.
stream_paused
bool stream_paused = false
If true, the sounds are paused. Setting stream_paused to false resumes all sounds.
Note: This property is automatically changed when exiting or entering the tree, or this node is paused (see Entity.run_mode).
volume_db
float volume_db = 0.0
Volume of sound, in decibels. This is an offset of the stream’s volume.
Note: To convert between decibel and linear energy (like most volume sliders do), use volume_linear, or @GlobalScope.db_to_linear() and @GlobalScope.linear_to_db().
volume_linear
float volume_linear
Volume of sound, as a linear value.
Note: This member modifies volume_db for convenience. The returned value is equivalent to the result of @GlobalScope.db_to_linear() on volume_db. Setting this member is equivalent to setting volume_db to the result of @GlobalScope.linear_to_db() on a value.
Method descriptions
Section titled “Method descriptions”get_playback_position
float get_playback_position()
Returns the position in the AudioStream of the latest sound, in seconds. Returns 0.0 if no sounds are playing.
Note: The position is not always accurate, as the AudioSystem does not mix audio every processed frame. To get more accurate results, add AudioSystem.get_time_since_last_mix() to the returned position.
Note: This method always returns 0.0 if the stream is an AudioStreamInteractive, since it can have multiple clips playing at once.
get_stream_playback
AudioStreamPlayback get_stream_playback()
Returns the latest AudioStreamPlayback of this node, usually the most recently created by play(). If no sounds are playing, this method fails and returns an empty playback.
has_stream_playback
bool has_stream_playback()
Returns true if any sound is active, even if stream_paused is set to true. See also playing and get_stream_playback().
play
void play(from_position: float = 0.0)
Plays a sound from the beginning, or the given from_position in seconds.
seek
void seek(to_position: float)
Restarts all sounds to be played from the given to_position, in seconds. Does nothing if no sounds are playing.
stop
void stop()
Stops all sounds from this node.
