Topic was automatically imported from the old Question2Answer platform.
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
# PLAY AUDIO
var stream = load(fileName)
$MyAudioSreamPlayer.stream = stream
$MyAudioSreamPlayer.play()
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.
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
buffer.remove(0)
stream.data = buffer
stream.format = 1 # 16 bit
stream.mix_rate = 44100
stream.stereo = true
file.close()
var streamer = get_node("/root/.../AudioStreamPlayer")
streamer.stream = stream
....
hope that can help you, in any case, you can contact me 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
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.