I have been making a very simple mp3 player and have gotten to the point I can add files through filesystem via clicking or control clicking In a filedialog. I can shuffle songs, save and load a playlist, it will also highlight the itemlist of the current song.
I can’t seem to nail down after trial and error how to just load a directory/folder and have it load the mp3s only by just selecting the folder. I tried Dir and Directory from a few examples but it either errors or ask if I mean DirAccess.
This is the one I have working thet has the one select or control select.
func _on_fd_open_files_selected(paths):
for path in paths:
var snd_file = FileAccess.open(path, FileAccess.READ) # Open path and read from the file
var stream = AudioStreamMP3.new() # Create a new MP3 Audio stream
stream.data = snd_file.get_buffer(snd_file.get_length()) # Load entire song into memory(not ideal)
snd_file.close() # Close file.
$AudioStreamPlayer.stream = stream # The loaded song in memory
$AudioStreamPlayer.play()
# Add the selected song to the queue
musicarray.append(path)
#Add songs to itemlist
var file_name_without_extension = path.get_file().get_basename()
$song_list.add_item(file_name_without_extension)
I thank you for the heads up on that. I am still having a go of it… but maybe like last time I might have a “oh this set of instructions”! kind of moment and have a burst of progress.
In all honesty I also need to tidy and reorganize the code as well. Redo the notes and everything. So the same notes aren’t used over and over and they are more concise.
Hi, I got folder loading to work. I’m sure this is no where near the correct method but it is working. Thank you again for the lead on the DirAccess.
## Function to load music from a folder
func _on_fd_ope_nfolder_dir_selected(path):
var dirc = DirAccess.open(path)
if dirc:
dirc.list_dir_begin()
var file_name = dirc.get_next()
while file_name != "":
if not dirc.current_is_dir():
# Check if the file is an MP3 file
if file_name.get_file().ends_with(".mp3"):
print("Found MP3 file: " + file_name)
# Add the file path to the music array
musicarray.append(dirc.get_current_dir() + "/" + file_name)
$song_list.add_item(file_name)
file_name = dirc.get_next()
# Play the first song if the array is not empty
if musicarray.size() > 0:
play_next_song()
else:
print("An error occurred when trying to access the path.")