+1 vote

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)
    $MyAudioSreamPlayer.stream = 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

in Engine by (197 points)

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.

3 Answers

–5 votes

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 = ImageTexture.new ()
var img = Image.new ()
img.load (fileName)
tex.create_from_image (img) # .import is created

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

enter image description here

preview tool:
enter image description here

enter image description here

by (197 points)

This isn't an answer, is it?

+2 votes

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 = File.new()
if file.file_exists(audio):
    file.open(audio, file.READ)
    var buffer = file.get_buffer(file.get_len())
    var stream = AudioStreamSample.new()
    for i in 200:
        buffer.remove(buffer.size()-1) # removes pop sound at the end
    stream.data = buffer
    stream.format = 1 # 16 bit
    stream.mix_rate = 44100
    stream.stereo = true
    var streamer = get_node("/root/.../AudioStreamPlayer")
    streamer.stream = stream

hope that can help you, in any case, you can contact me ;) bye

by (111 points)

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: https://github.com/godotengine/godot-proposals/issues/732

+1 vote

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

There's a proposal about this issue in github:

by (64 points)

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?

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

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.