You have to create an AudioStreamSample and set some properties that have to match your file.
var audio = "path to your file"
func load_audio_from_user_dir():
var file = File.new()
if file.file_exists(audio):
file.open(audio, file.READ)
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
buffer.remove(0)
stream.data = buffer
stream.format = 1 # 16 bit
stream.mix_rate = 44100
stream.stereo = true
file.close()
$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 for
loop.