Playing sounds with an AudioStreamPlayer using variable volume

Godot Version



Is it possible to set the the volume when playing a sound through an AudioStreamPlayer? The AudioStreamPlayer goes through a bus in a mixer, and is used for other sounds too, and I don’t want to their volumes to be affected. Here’s how I used to do this in Unity:

audioSource.PlayOneShot(audioClip, volume);


You can set the volume with AudioStreamPlayer.volume_db before calling

Thank you, but the volume is intended just for this playthrough of this particular sound, I might play need to play another sound using the same AudioStreamPlayer at another volume later, just like with PlayOneShot().

Then you change the to point to the other sound and change its volume too with AudioStreamPlayer.volume_db before playing the other sound.

You can create a function like:

func play_sound(sound, volume = 0.0):
	$ = sound
	$AudioStreamPlayer.volume_db = volume

The problem with this approach is that it will cause any sound currently being played to also have its volume affected. I figured there was a similar solution like PlayOneShot() that I just couldn’t find - but perhaps there isn’t.

create a new audiostreamplayer for each new sfx spawned and assign the sound to stream, destroy it after the sounds ended playing

Thanks - yes, that’s definitely an option, just seems a bit wasteful for oneshot sounds that will be played a ton…

you can specifically make audiostreamplayer to play only a sound, but problem will be if somehow there need to play that exact sound many times in a single moment, it cant do that. So it really just need to make a new audiostreamplayer for each new sfx sound, queue_free it after it’s done playing

It’s possible to use one AudioStreamPlayer to play a single sound multiple times without cutting off the old one by setting AudioStreamPlayer.max_polyphony to a higher value than the default 1.