AudioStreamPlayer ignores timer, plays repeatedly until it finishes

Godot Version

4.2.2

Question

I’m trying to make an AudioStreamPlayer play a sound repeatedly with a slight delay between each sound. I have it set up so that it should theoretically pick a sound, play it, wait for it to finish, then repeat until it’s done it enough times. Instead it plays it seemingly every frame, with no clear regard for the timer.

Here’s the code I have right now:

if Globals.health > health_prev_frame:
		for i in (Globals.health - health_prev_frame):
			$AudioStreamPlayer.stream = load("res://sounds/Heal" + str((randi() % 3) + 1) + ".wav")
			$AudioStreamPlayer.play()
			#await $AudioStreamPlayer.finished
			await get_tree().create_timer(($AudioStreamPlayer.stream.get_length())).timeout

I assume this code is part of your _process function? If so, it’s run every frame – and nothing stops play() from restarting the stream every time!

Use $AudioStreamPlayer.playing to check if it’s “safe” to call play.

1 Like