Audiostream only plays when the input is released

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By dany_pitre

Hi there !

I have a problem that I never had before.

I want my player to make a footstep sound when he walks. So I simply made this in my process function, where my motion is managed:

	if motion.x != 0 or motion.y != 0:

the problem that I have is that the sound only plays when i release the key. It’s really weird. At first i was thinking that maybe the file is too heavy and I need to preload it, but it plays exactly at the second i release the input…

Any advice ? thanks !!

:bust_in_silhouette: Reply From: Lola


calling play on an AudioStreamPlayer resets it to the begining each time, so in your case the sound only plays at the moment you release the key because it is precisely the moment you stop asking it to rewind every frame.

You can get around this by checking if your AudioStreamPlayer is playing before re-ordering it to play the sound (using AudioStreamPlayer.playing).

My friend, you just saved my game !

Thanks a lot :slight_smile:

dany_pitre | 2021-09-19 20:06

:bust_in_silhouette: Reply From: Skelteon

Is the beginning of your sound effect quiet?

Looking at the code, I’d expect it to begin playing your sound effect, from the start infinitely until motion isn’t happening anymore (motion != Vector2.ZERO just btw :wink: ), then play the sound clip all the way through.

Also as a side-note to @Lola’s comment: you can get the current play position in an AudioStreamPlayer with get_playback_position() and pass the result as an argument to play(from_position). This probably isn’t necessary for your use case, but is still a good-to-know :).