0 votes

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
Godot version Gogot 3.5v
in Engine by (27 points)

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

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

What error do you get?

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

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.

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.

1 Answer

+2 votes

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()
by (14,866 points)

Can Godot handle mp3s?

Yes, as demonstrated above...

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).

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.