Guide: How to load WAV, MP3 or OGG files on runtime from res:// directory

Anyone wanted to know how to actually play audio files from game’s resources directory, here’s step by step instruction:

  1. Create a script with whatever name you want (for example audio.gd) and add it to Project Settings > Globals.
  2. Put the code below inside it:
extends Node

func play_sound(sound: String, volume_db:=0.0, pitch_scale:=1.0, max_polyphony:=1, bus:="Master"):
	var soundinstance = AudioStreamPlayer.new()
	add_child(soundinstance)
	soundinstance.stream = load(sound)
	soundinstance.bus = bus
	soundinstance.volume_db = volume_db
	soundinstance.pitch_scale = pitch_scale
	soundinstance.max_polyphony = max_polyphony
	
	soundinstance.play()
	await soundinstance.finished
	soundinstance.queue_free()
  1. Make sure to check if option “Export all resources in the project” is selected in the Export window > Resources.
  2. Now you can call it from any script like this:
Audio.play_sound("res://example.wav")

Also it should work when the project is exported (Tested in 4.4.stable).

If you are exporting your audio files wouldn’t load("res://example.wav") work?

var soundinstance := AudioStreamPlayer.new()
add_child(soundinstance)
soundinstance.stream = load(sound)

Interesting config file reading though, could be useful if you do not have the full path of the resource and need to get it dynamically, though 4.4 should fix this with ResourceLoader.list_directory

oh, you can actually do that? didn’t know that, but that way works as well

Yes the .remap/.import problem is tough, usually comes up when one tries to dynamically load filepaths, like picking a random song from a folder, especially a folder that is added to by DLC or mods; but load takes normal “res://” paths just as preload does.

How did you run into .remap/.import files?

I was trying to play sounds on runtime, first I just called AudioStreamWAV.load_from_file(sound) thing to soundinstance.stream, but that didn’t work. I’ve spent a lot of time figuring out the actual way to do that (and on the internet there was literally NO solution I needed). After a while I looked up in exported runnable file through GDRE and found that in .godot/imported there were still files I was looking for. So I just parsed the real path to them, and then a lot of time was wasted on making the files actually play from those paths. When I was testing it, the autocompletion suggested something called “ResourceLoader”, and that how I’ve got here.

I see, yeah AudioStreamWAV.load_from_file does work in-development because the raw wav file exists in your project directory, but it’s converted/remapped on export.

AudioStreamWAV.load_from_file is for dynamically loading files, such as user content like if you wanted to play a random song from a folder in “user://” or any file on the player’s disk. load/preload is for loading packed files, generally anything in “res://” exported or in-development should work.