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. Specifically 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:
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);
}
@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;
}
For runtime loading of audiofiles from filesystem you can use AudioImporter:
Use the bass audio library plugin to support more formats.
async Task LoadAudioAtRunTime(string audioURL)
{
AudioClip audioData = null;
string audioPathInLocalSource = "";
try
{
bool isDownloadingSuccess = true;
bool isFinishDownloading = false;
int lastIndex = audioURL.LastIndexOf("/", StringComparison.Ordinal);
string audioName = audioURL.Remove(0, lastIndex + 1);
audioPathInLocalSource = PathConfig.MEDIA_CACHE + audioName; // path to model fill
string audioExtension = audioName.Remove(0, audioName.LastIndexOf(".", StringComparison.Ordinal) + 1).ToLower();
if (!File.Exists(audioPathInLocalSource))
{
Debug.Log("file not exist");
// If model was not downloaded (not exist in local), then download it
audioData = await UnityHttpClient.GetAudioClip(audioURL);
// Save it
Debug.Log("SAVING AUDIO FILE INTO: " + audioPathInLocalSource);
bool isSuccess = SavWav.Save(audioPathInLocalSource, audioData);
Debug.Log("AUDIO SAVE PATH: " + audioPathInLocalSource);
if(isSuccess){
Debug.Log("AUDIO SAVED COMPLETEED");
}
else{
Debug.Log("AUDIO SAVE FAILED");
}
return audioData;
}
else
{
isFinishDownloading = true;
isDownloadingSuccess = true;
}
if (isDownloadingSuccess)
{
// Load file from local to unity application
// LoadObjectFromLocal(modelPathInLocalSource, modelExtension);
return audioData;
}
}
catch (Exception exception)
{
Debug.Log("AUDIO SAVE PATH: " + audioPathInLocalSource);
Debug.Log($"Minh LH error loading AUDIO {exception.Message}");
}
return audioData;
}