Array of audio files working in editor but not when exported

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

So I have a AudioStreamPlayer, that has script that iterates through a folder and pulls all the wav files and stores them in an array and then a method to play one of them at random and another to skip to the next song. When I do this in the editor it works with no issues or errors, but as soon as I export it to a runnable windows exe it fails to retrieve any of the files and shows a error of
Invalid get index ‘0’ (on base: ‘Array’).
At: res://World/MusicPlayer.gdc:32

this is the code below im using

extends AudioStreamPlayer
var music_list = []

func _ready():

func _input(event):
if event.is_action_pressed(“skip_song”):

func get_all_music():
var dir =“res://Assets/Music/”)

while true:
	var file = dir.get_next()
	if file == "":
		#no more files
	elif not file.begins_with(".") && !file.ends_with(".import"):
		music_list.append(load("res://Assets/Music/" + file))


func pick_song():
var list_len = music_list.size()
var i = randi() % (list_len - 1)
stream = music_list[i]

func _on_MusicPlayer_finished():

:bust_in_silhouette: Reply From: Calinou

There are two things to check for here:

  1. Add *.ogg,*.mp3,*.wav to the non-resource export filter in the Export dialog. This ensures the audio files you’re trying to load at run-time are present in the PCK file.
  2. Check the file casing in the file names. Windows is not case-sensitive but the virtual PCK filesystem (which is used in export projects) is case-sensitive. I recommend using lowercase characters only in file names to prevent problems like these from cropping up.

if the music file name has spaces in it could that cause issues too?

sir306 | 2021-07-05 13:05

Spaces should be fine, but it’s best to avoid them if possible.

Calinou | 2021-07-05 20:58

I’m exporting all files and it appears that all files are being called correctly

sir306 | 2021-07-06 02:25

:bust_in_silhouette: Reply From: tubiky

I had the same problem. You can go around by appending all the “.import” files into the array and getting rid of the “.import” extension when you load audiostream.

var file_name = array[0].replace(".import", "") = load(AUDIO_FILE_PATH + file_name)

But I have no clue why this weird thing happened. I read several articles saying that copying the project folder here and there might cause the problem, which I did.
Hope this answer hlep others who might have similar problems with other audio extensions such as ogg, and wav.

P.S. : This is my first answering a question in this community though.
Hope Godot gets more popular in the near future. : )