Upload audio files using FileDialog.
Asked Answered
V

6

0

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!

Vanillin answered 27/9, 2023 at 16:55 Comment(0)
L
0

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()
Liggitt answered 28/9, 2023 at 3:58 Comment(0)
S
0

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

Stannary answered 27/9, 2023 at 17:0 Comment(0)
V
0

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

Vanillin answered 28/9, 2023 at 3:8 Comment(0)
L
0

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()
Liggitt answered 28/9, 2023 at 3:58 Comment(0)
B
1

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.

Barfuss answered 28/9, 2023 at 4:49 Comment(0)
V
0

Liggitt

It works! Thank you so much! :-)

Vanillin answered 28/9, 2023 at 7:2 Comment(0)
L
0

Barfuss

We're getting a load_from_file method in 4.2. I'll wait for that.

Liggitt answered 28/9, 2023 at 13:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.