How to load a file from pc's file system?

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

I am trying to load an audio file from my PC filesystem with FileDialog node and load the file by the load() method, but it returns a null, so how can I implement this?

func _on_FileDialog_file_selected(path):
   #The path /home/me/Music/Classic.mp3
    audioSreamPlayer.stream = load(filePaht)
    audioSreamPlayer.play

You’ve passed in path but are referencing filePaht??

jgodfrey | 2022-09-29 17:44

Oh sorry, but this is not the problem, I just typed it wrong.

func _on_FileDialog_file_selected(path):
#The path /home/me/Music/Classic.mp3
audioSreamPlayer.stream = load(path)
audioSreamPlayer.play

Atef | 2022-09-29 17:52

What error do you get?

SteveSmith | 2022-09-29 18:18

The is no error, the load(path) method doesn’t load the path and it returns null.

Atef | 2022-09-30 08:46

Are you sure there’s nothing in either the dos console (if running in Windows) or the Output or Debugger windows in Godot? You can’t normally just call “load” on any old file. And I don’t think Godot can handle mp3s either.

SteveSmith | 2022-09-30 10:25

yeah you’re right there is an error in the Debugger window: E 0:00:19.073 _load: No loader found for resource: /path/to/file.mp3.

Atef | 2022-09-30 10:55

:bust_in_silhouette: Reply From: jgodfrey

Since you’re trying to load a raw, filesystem-based file (as opposed to an imported resource), I think you’ll have to work a little harder. Something like this should work (for example):

var path = "/path/to/some.mp3"
var snd_file=File.new()
snd_file.open(path, File.READ)
var stream = AudioStreamMP3.new()
stream.data = snd_file.get_buffer(snd_file.get_len())
snd_file.close()
$AudioStreamPlayer.stream = stream
$AudioStreamPlayer.play()

Can Godot handle mp3s?

SteveSmith | 2022-09-30 10:24

Yes, as demonstrated above…

jgodfrey | 2022-09-30 18:02

Cool. I didn’t know this node existed. All the google results for “godot mp3” say it’s not supported (but they are quite old now).

SteveSmith | 2022-09-30 18:06