How can I get AudioStreamPlayer to play sounds?

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

Hi, I’m trying to convert a game from Godot 2.1 to 3.4 and I’m having an issue with the audio. Godot 2.1 had a SamplePlayer class that I’m trying to re-create using AudioStreamPlayers but so far I can’t get any sounds to actually play.

I’m basically creating a stream player for each sound effect in a directory and creating a dictionary of all the sounds:

used_player = AudioStreamPlayer.new()
used_player.stream = load("res://assets/effects/%s" % file_name)
used_player.set_bus("Effects")
# store the player keyed by the name
effect_players[effect_name] = used_player

So later I can do something like this:

func play(name):
    if(effect_players.has(name)):
        effect_players[name].play()
    else:
        print("Trying to play missing effect: %s" % name)

I’ve verified the loaded files are of class AudioStreamSample and printed the stream.data.size() to verify they’re loaded but still nothing. Audio buses are all at default and all the files are .wav. Any ideas?

:bust_in_silhouette: Reply From: bepis

Nvm… Apparently you have to add the stream players to the scene to actually be able to use them.

add_child(used_player)

1 Like