Help with "import audio using gdscript"

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

I have a script that allows the user to select an audio file (ogg or wav) and play it. If this file is outside the project folder, then copy it to the folder project:

var fileName = "res://Audio/SE/%s" % current_file
if !dir.file_exists(fileName):
	current_file = current_file.to_lower()
	dir.copy(path, fileName)
	# TODO: create import
	var stream = load(fileName)
	$ = stream

but you can not play it because .import file is missing. Then, how i create this import file to this audio?

Note: .import file is created for this new audio when i click over godot editor main window but i need create it just after line dir.copy(path, fileName) in my script

Sorry I’m not familiar with importing resources at runtime, however just wanted to let you know that adding files to the project folder after the project has been exported will not work.

BraindeadBZH | 2019-07-13 14:45

:bust_in_silhouette: Reply From: newold

my script is a tool used to create a database for a dialog system and is only used while you are creating your game with godot editor.

I have been able to import images using this:

var tex = ()
var img = ()
img.load (fileName)
tex.create_from_image (img) # .import is created

But I do not know how to do it with audio files


preview tool:

This isn’t an answer, is it?

bluenote | 2020-05-21 20:40

:bust_in_silhouette: Reply From: uriel

hi, i found the way to get audiofiles loaded without importing them:
check this out and accomodate at your needs…

    var path = str(path)
var fname = str(audiofile(id))
var audio = path+"/"+fname

var file =
if file.file_exists(audio):, file.READ)
	var buffer = file.get_buffer(file.get_len())
	var stream =
	for i in 200:
		buffer.remove(buffer.size()-1) # removes pop sound at the end
		buffer.remove(0) = buffer
	stream.format = 1 # 16 bit
	stream.mix_rate = 44100
	stream.stereo = true
	var streamer = get_node("/root/.../AudioStreamPlayer") = stream

hope that can help you, in any case, you can contact me :wink: bye

This code simply ignores the wave header, or worse, it even uses the wave header as audio data as well. Since the header is tiny, it only leads to a short noise. This hack only “works” when all assumptions about the wave file are right, and breaks e.g. for all wave files with different bit rates / number of channels. The proper solution is in this proposal: Easily load external audio (WAV) files at run-time · Issue #732 · godotengine/godot-proposals · GitHub

bluenote | 2020-05-21 21:29

:bust_in_silhouette: Reply From: Gianclgar

I had a similar issue and ended up making this: Hope it’s useful!

There’s a proposal about this issue in github:

I have successfully used your solution and it works fine in Windows with playing a WAV file without importing (no .import file). However I am getting no sound when I run the same code in Android. Any ideas about what I can look at to correct this?

Bob Dolan | 2021-02-14 13:38

This worked great for me in Windows when I needed to import .wav and .mp3 audio files from a file system path.

josephcantrell14 | 2022-02-27 05:51