0 votes

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/sfxsoundsfanfare3.wav")

func _process(delta: float) -> void:
$AudioStreamPlayer2D.stream = CorrectSound
$AudioStreamPlayer2D.play()

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?

in Engine by (153 points)

1 Answer

0 votes

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():
        $AudioStreamPlayer2D.stream = CorrectSound
        $AudioStreamPlayer2D.play()

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...

by (10,756 points)
edited by

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.

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?

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.

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:

Node2D
    AudioStreamPlayer2D

And, the Node2D has this script attached:

extends Node2D

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

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

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

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.

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?

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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.

Categories