Correct way to load and play sounds from code

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

I have been trying to play sounds in my game for a week, and cant get any result. If I assign the audio file directly to the AudioStreamPlayer2D in the editor, it works. But, if I try to load the audio clip and assign it on code, I dont get any sounds. This is my code:

var CorrectSound = preload(“res://Sounds/sfx_sounds_fanfare3.wav”)

func _process(delta: float) → void:
$ = CorrectSound

I have tried load instead of preload. I have tried moving the code to another script. I tried with export, then assigning the value in the editor, and nothing. So, I guess Im not doing it the right way. Whats my error here?

:bust_in_silhouette: Reply From: jgodfrey

Your basic code is generally fine. The problem is that you’re (re)assigning and (re)playing the sound in _process(), which runs in each frame.

If you need to do it from _process() you probably want something like this:

func _process(delta: float) -> void:
	if !$AudioStreamPlayer2D.is_playing():
		$ = CorrectSound

That’ll only assign and play the sound if its not already playing. So, if it’s already playing, just let it happen. Otherwise, assign the file and start it playing…

I tested this and didnt work. I have to say that I not only play the audio in _process, but also in another function too, withouth results.

rogerdv | 2020-11-28 15:48

I tested this too, and it does work. Are you sure the underlying audio file is OK? For example, if you select it in the File System panel, can you successfully play it from the audio controls at the bottom of the Inspector panel?

jgodfrey | 2020-11-28 15:51

I think it works. I have sound working on the same project, but it is assigned directly to the audio player in the editor. The problem is only in code, thats why I think Im doing something wrong. The files play fine, so I dont think there is any problem there.

rogerdv | 2020-11-28 22:12

Not sure what “I think it works” means. You can easily check as mentioned above…

Anyway, I created a new, test scene consisting of this scene tree:


And, the Node2D has this script attached:

extends Node2D

var CorrectSound = preload("res://chimes.wav")

func _process(delta: float) -> void:
	if !$AudioStreamPlayer2D.is_playing():
		$ = CorrectSound

The attached sound file plays in full, and then repeats permanently - as expected…

jgodfrey | 2020-11-28 22:20

Well, I did the same test, and some others. The problem is not my code, but the scene. If I assign the audio file directly to the AudioStreamPlayer, it doesnt works. I tried assigning the audio to a player in the parent scene, neither worked, not even if I place the play() inj the ready function.
I thought it could be because my scene root node was a Control, but made a test in a separate scene and it works. Cant find the reason why it doesnt works in this particular scene. The only different thing here is that I instance it instead of loading from another, because it is a window I display with some questions.

rogerdv | 2020-11-30 14:40

Hmmm… I guess I don’t really have much else to say without being able to see the project itself. Is it available anywhere for inspection?

jgodfrey | 2020-11-30 14:46

I have it on github, I can give you access if you want, just give me your user, or I can make it public.

rogerdv | 2020-11-30 18:52