Here's an example function I whipped up. StartCoroutine("LoadFiles", "DirectoryWithMusicFiles"); - one problem with this method is that Unity apparently disallows MP3s for windows (and linux I'm guessing) standalone builds. .wav & .ogg both work though.
It uses the application data path as root directory right now (which is Assets in editor mode). So you if you add a MUSIC folder to Assets for testing, just pass "MUSIC" in to the location paramater.
One issue left to solve is actually saving the playlists, they're currently just being discarded in the function.
Code: Select all
IEnumerator LoadFiles(string location)
{
string path = Path.Combine(Application.dataPath, location);
if(!Directory.Exists(path))
{
Debug.LogWarning("Couldn't locate external music files at " + path);
yield break;
}
var files = Directory.GetFiles(path, "*", SearchOption.TopDirectoryOnly);
//filter out unplayable files
files = files.Where(file => file.ToLower().EndsWith(".wav") || file.ToLower().EndsWith(".ogg")).ToArray();
//no music files to play
if (files == null || files.Length < 1)
yield break;
AudioClip[] songList = new AudioClip[files.Length]; //need solution to save playlists
AudioClip clip;
WWW www;
int count = 0;
foreach(string filePath in files)
{
www = new WWW("file://" + filePath);
while (!www.isDone)
yield return new WaitForEndOfFrame();
clip = www.GetAudioClip(false,false);
while (clip.loadState == AudioDataLoadState.Loading)
yield return new WaitForEndOfFrame();
if(clip != null && clip.loadState == AudioDataLoadState.Loaded)
songList[count++] = clip;
www.Dispose();
}
}