You have to create an AudioStreamSample and set some properties that have to match your file.
var audio = "path to your file"
var file = File.new()
var buffer = file.get_buffer(file.get_len())
var stream = AudioStreamSample.new()
for i in 200:
buffer.remove(buffer.size()-1) # removes pop sound at the end
stream.data = buffer
stream.format = 1 # 16 bit
stream.mix_rate = 44100
stream.stereo = true
$AudioStreamPlayer.stream = stream
I'm not sure why but when loading a sample this way, it has a click/pop sound at the end. The
for loop seems to fix that.
Edit: I noticed that some samples have the click/pop sound also at the start, so I added a second line to the