Creating AudioStream on the fly - Condition err is true

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Soaku
:warning: Old Version Published before Godot 3 was released.

I’m having hard time with StreamPlayer component. I’ve created one to play menu music with the audio set through the editor. I also created other component and tried to play music downloaded from my website. Here it does not works.

I’ve tried creating new AudioStreamOGGVorbis object, setting its path to the file and then assigning it to StreamPlayer:

var player = get_node("../StreamPlayer")
player.stop()
var stream = AudioStreamOGGVorbis.new()
stream.set_path("user://assets/"+todo[0][1])
#Also tried using set()
player.set_stream(stream)
player.play()

Then I get 3 errors:

Condition ’ err ’ is true, returned: err
Condition ’ p_channels != 1 && p_channels != 2 && p_channels != 4 && p_channels != 6 ’ is true, returned: ERR_INVALID_PARAMETER
Condition ’ !is_inside_tree() ’ is true

I also tried changing path of previously used object, created in editor:

var player = get_node("../StreamPlayer")
player.stop()
var stream = player.get_stream()
stream.set_path("user://assets/"+todo[0][1])
#As previously said, I also tried using set()
player.set_stream(stream)
player.play()

Here, I get no errors, but it plays the previously set song. When I did print(player.get_stream().get_path()) between set_stream() and play() the path I set it to.

PS: the todo var is previously defined and is correct.

Please, help. I really don’t know why it does not work.

:bust_in_silhouette: Reply From: Soaku

I found out what I was doing wrong.

Instead of creating an object I should have used the load() function:

var vstream = load("user://assets/"+todo[0][1])
nplayer.set_stream(vstream)
get_node("..").add_child(nplayer)
nplayer.add_to_group("streams")
nplayer.play()

Note: I changed my mind and to prevent songs to load every time it is needed I created new node for each of those…