- Home /
Load audioclip from folder on computer into game, in runtime
Hello I want the player to specify the music in the game, and for that i need to load it somehow from a folder on the computer. Specificly i want to load all audioclips of mp3 from a specific folder. Probably the same as the game folder, then import them into the game somehow. I've been researching a bit and found this: https://docs.unity3d.com/ScriptReference/WWW.GetAudioClip.html
But how does one use this correctly and actually set some kind of prefix to the correct folder?
UPDATE:
Unity doesn't support mp3 it looks like so i converted to .wav I found a post on unity forums and here is my code. Note that his still doesnt give me anything, i dont get an error but the debug message is empty so it doesnt find the file correctly?
Any help is welcome
public AudioClip[] clips;
private string musicDir = "C://Users//Ägaren//Documents//FluxClips";
AudioSource source;
void Awake ()
{
source = GetComponent<AudioSource> ();
}
void Start ()
{
//AudioClip selectedClip = clips [Random.Range (0, clips.Length)];
WWW audioLoader = new WWW ("file://" + musicDir + "//clip.wav");
AudioClip selectedClip = audioLoader.GetAudioClip (false, false, AudioType.WAV);
source.clip = selectedClip;
Debug.Log (source.clip.name);
}
Answer by neoRiley · Mar 13, 2020 at 04:52 PM
@fluxhackspro - thanks for the reminder on how to load from the file system - I thought I'd throw this out as a possible answer as well with the updated UnityWebRequest/UnityWebRequestMultimedia object with an async example:
async void Start()
{
// build your absolute path
var path = Path.Combine(Application.dataPath, "Audio", "sounds", "myAudioClip.wav");
// wait for the load and set your property
CurrentClip = await LoadClip(path);
//... do something with it
}
async Task<AudioClip> LoadClip(string path)
{
AudioClip clip = null;
using (UnityWebRequest uwr = UnityWebRequestMultimedia.GetAudioClip(path, AudioType.WAV))
{
uwr.SendWebRequest();
// wrap tasks in try/catch, otherwise it'll fail silently
try
{
while (!uwr.isDone) await Task.Delay(5);
if (uwr.isNetworkError || uwr.isHttpError) Debug.Log($"{uwr.error}");
else
{
clip = DownloadHandlerAudioClip.GetContent(uwr);
}
}
catch (Exception err)
{
Debug.Log($"{err.Message}, {err.StackTrace}");
}
}
return clip;
}
This worked for me on Windows, but not on Android. For Android it worked once I prefixed the path with the proper "file://" uri scheme. e.g.
var builder = new UriBuilder(path) {Scheme = Uri.UriSchemeFile};
var uri = builder.ToString();
using (UnityWebRequest uwr = UnityWebRequestMultimedia.GetAudioClip(uri, AudioType.WAV))
Answer by mgstauff · Nov 15, 2020 at 07:41 PM
For runtime loading of audiofiles from filesystem you can use AudioImporter: https://github.com/Hello-Meow/AudioImporter
Use the bass audio library plugin to support more formats.
Answer by Flavosky · Feb 08, 2021 at 10:29 PM
Hello everyone,
I would like to add Audio Importer to a Game Object and when I right click on it, that opens the Audio Importer Browser. An idea ?
(I am Sound Designer and C# is pretty difficult to me so.. :/)
Thanks a lot !