Topic was automatically imported from the old Question2Answer platform.
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():
get_all_music()
pick_song()
func _input(event):
if event.is_action_pressed(“skip_song”):
pick_song()
func get_all_music():
var dir = Directory.new()
dir.open(“res://Assets/Music/”)
dir.list_dir_begin()
while true:
var file = dir.get_next()
if file == "":
#no more files
break
elif not file.begins_with(".") && !file.ends_with(".import"):
music_list.append(load("res://Assets/Music/" + file))
dir.list_dir_end()
func pick_song():
randomize()
var list_len = music_list.size()
var i = randi() % (list_len - 1)
stream = music_list[i]
print(music_list[i])
play(0)
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.
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
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", "")
audio.stream = 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. : )
Thank you so much!!! I just created an account to thank you for this answer!
I had a similar issue where I was attempting to load an array of music, but it kept failing on export. The change was using the .import path instead of the .mp3/.ogg/.wav files. Very strange that this is still an issue in Godot 4.4 haha.
I will forward people to this answer in the future! Thank you!!!