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.")
# GoDot 4.4.1 Stable
#
# Call / Execute function with:
# var mylist = _buildPathContents("C:/my/path/to/some/directory")
#
# [ Example Output ]
# for _key in mylist :
# print(_key)
# for i in range(mylist[_key].size()):
# print("\t" + mylist[_key][i])
#
func _buildPathContents(_path: String, _tempfiletree: Dictionary = {}) -> Dictionary:
# Print files/directories into console window (only if GoDot is running Debug Mode)
var _printDebug = false
# Open path so we can view contents
var _dir = DirAccess.open(_path)
# Check if we gained access to the path
if _dir:
# Make sure we have a key to append files into
if not _tempfiletree.has(_path):
_tempfiletree[_path] = []
# Begin iterating
_dir.list_dir_begin()
# Get first file or directory
var _filename = _dir.get_next()
# Loop as long as we have a name for something
while _filename != "":
if _dir.current_is_dir():
if OS.is_debug_build() and _printDebug:
print("Dir: " + _filename)
# Call self for subdirectory
_tempfiletree = _buildPathContents(_path.path_join(_filename), _tempfiletree)
else:
# Add file to array
_tempfiletree[_dir.get_current_dir()].append(_filename)
if OS.is_debug_build() and _printDebug:
print("File: " + _filename)
# Get next file or directory
_filename = _dir.get_next()
# End iterating
_dir.list_dir_end()
return _tempfiletree
You can make it a coroutine in-case you need to sync the exchange:
Call with: var mylist = await _buildPathContents("C:/my/path/to/some/directory")
It is not ‘proper’ but it works and works well, use for any number of file-tree tasks. With some alteration, customize to only return specific files or file types even.