How to create a AudioStreamPlayer in GDScript

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

I am trying to play a .ogg music file from gdscript but nothing happens (no sound is played when I do this). If someone knows what I am doing wrong I would be very glad.

var music = AudioStreamPlayer.new()
var stream = load("res://Assets/Music/House In a Forest Loop.ogg")
music.set_stream(stream)
music.volume_db = 1
music.pitch_scale = 1
music.play()

I run this in the _ready of the scene I am viewing.
Godot version: 3.0.4
OS: x86_64 Linux 4.9.86-1-MANJARO

:bust_in_silhouette: Reply From: volzhs

AudioStreamPlayer is a Node so it won’t do anything before added to scene.
you just forgot to do add_child(music)

Thank you, I worked with libgdx before and didn’t catch that only nodes are updated in Godot and the AudioStreamplayer has to be part of the scene.

bambelu | 2018-07-21 15:47