Help with AudioStreamPlayer

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

Hello,
I am new to Godot and I want an object to play a simple sound before disappearing. So I added an AudioStreamPlayer node to my object, dragged and dropped a wav file to the Stream box. I wrote the following code but it doesn’t play. Any ideas?

func _on_block_brk_1_input_event(viewport: Node, event: InputEvent, shape_idx: int) -> void:
#print("Any input event")
if event is InputEventMouseButton:
	if event.is_pressed():
		print("Object clicked")
		$AudioStreamPlayer.play()
		queue_free()
:bust_in_silhouette: Reply From: njamster

$AudioStreamPlayer.play() will start playing the sound, but won’t wait until it’s finished. So you start playing the sound and then immediately delete the object.

This shoukld work:

$AudioStreamPlayer.play()
yield($AudioStreamPlayer, "finished")
queue_free()