I have a problem with FileDialog that I can't seem to resolve: in essence, I would like to upload an external audio file onto an AudioStreamPlayer node using FileDialog, but all my attempts have been unsuccessful.
Do you have any tips for me? Thank you very much!

  • Toxe replied to this.
  • This loads and plays a file for me.

    var ap:AudioStreamPlayer
    
    
    func _ready():
    	var fd = FileDialog.new()
    	add_child(fd)
    	fd.position = Vector2(100, 100)
    	fd.size = Vector2(800, 500)
    	fd.file_mode = FileDialog.FILE_MODE_OPEN_ANY
    	fd.visible = true
    	fd.access = FileDialog.ACCESS_FILESYSTEM
    	fd.connect('file_selected', show_me)
    	
    	ap = AudioStreamPlayer.new()
    	add_child(ap)
    
    
    func show_me(file):
    	if file and file.right(4) == '.ogg':
    		print(file)
    		var ogg = load(file)
    		print(ogg)
    		ap.stream = ogg
    		ap.play()

    Edit: I can get this to work for mp3s that are not in the project, but not for ogg.

    func show_me(file):
    	if file and file.right(4) == '.mp3':
    		print(file)
    		var dat = FileAccess.get_file_as_bytes(file)
    		var mp3 = AudioStreamMP3.new()
    		mp3.data = dat
    		ap.stream = mp3
    		ap.play()

    Rob1980 Can you describe the problems you are running into a bit more? Is there any code you can share?

      Toxe
      Sure! So, I have an AudioStreamPlayer and a FileDialog nodes. The goal is to upload an external audio file into the AudioStreamPlayer using the FileDialog.
      I've tried some codes, like this one:
      func _on_FileDialog_file_selected(path):
      var song : AudioStream = load(path)
      $AudioStreamPlayer.stream(song)
      $AudioStreamPlayer.play()
      or this one:
      func _on_file_dialog_file_selected(path):
      $AudioStreamPlayer.stream = load(path)
      $AudioStreamPlayer.play()
      but no use! I've run out of ideas now, and I don't know what to do anymore.

      This loads and plays a file for me.

      var ap:AudioStreamPlayer
      
      
      func _ready():
      	var fd = FileDialog.new()
      	add_child(fd)
      	fd.position = Vector2(100, 100)
      	fd.size = Vector2(800, 500)
      	fd.file_mode = FileDialog.FILE_MODE_OPEN_ANY
      	fd.visible = true
      	fd.access = FileDialog.ACCESS_FILESYSTEM
      	fd.connect('file_selected', show_me)
      	
      	ap = AudioStreamPlayer.new()
      	add_child(ap)
      
      
      func show_me(file):
      	if file and file.right(4) == '.ogg':
      		print(file)
      		var ogg = load(file)
      		print(ogg)
      		ap.stream = ogg
      		ap.play()

      Edit: I can get this to work for mp3s that are not in the project, but not for ogg.

      func show_me(file):
      	if file and file.right(4) == '.mp3':
      		print(file)
      		var dat = FileAccess.get_file_as_bytes(file)
      		var mp3 = AudioStreamMP3.new()
      		mp3.data = dat
      		ap.stream = mp3
      		ap.play()

        load requires a resource be in the project. See https://docs.godotengine.org/en/stable/classes/class_resourceloader.html

        Note: You have to import the files into the engine first to load them using load. If you want to load Images at run-time, you may use Image.load. If you want to import audio files, you can use the snippet described in AudioStreamMP3.data.

        For loading oggs, you might be able to use that same FileAccess strategy but with AudioStreamOggVorbis. I don't actually know for sure. Haven't tried it myself.