- Home /
Identical files in two locations needed to play music loaded at runtime?
Hi, I've written a test program that loads .mp3 and .ogg files from a folder outside of the Unity Assets folder. Users can add their own music to the folder, and a GUI button will pop up with the file name; click and play. This works well, (the .mp3s only load in the executable) except that I need to have each file in the "Music" folder I created, as well as the current directory of the exe.
Perhaps I'm doing something stupid in my code, or Unity will only load files in the same directory as the exe during runtime.
Here's the script:
void Start ()
{
ReadDirectory();
}
void ReadDirectory()
{
string path = Directory.GetCurrentDirectory()+ "\\Music\\";
DirectoryInfo info = new DirectoryInfo(path);
testDirectory = path;
FileInfo[] fileInfo = info.GetFiles();
foreach (FileInfo file in fileInfo)
{
string filename = file.ToString();
if (filename.Contains(".mp3") || filename.Contains(".ogg"))
{
fileList.Add(filename);
filename = filename.Replace(path, "");
filename = filename.Replace("\\", "");
abbrevFileList.Add(filename);
}
}
}
void Update () {
if (audio.clip)
{
if (!audio.isPlaying && audio.clip.isReadyToPlay)
{
audio.Play();
}
}
}
void OnGUI()
{
// to see that the directory is correct and allow a way to close the application
if (GUI.Button(new Rect(Screen.width * 0.4f, Screen.height * 0.3f, Screen.width * 0.2f, Screen.height * 0.05f), testDirectory))
{
Application.Quit();
}
// select file
for (int i = 0; i < fileList.Count; ++i)
{
if (GUI.Button(new Rect(Screen.width * 0.4f, (Screen.height * 0.4f) + (((float)i) * (Screen.height * 0.1f)), Screen.width * 0.2f, Screen.height * 0.05f), abbrevFileList[i]))
{
WWW www = new WWW("file://"+fileList[i]);
if (fileList[i].Contains(".mp3"))
audio.clip = www.GetAudioClip(false, true, AudioType.MPEG);
else
{
audio.clip = www.GetAudioClip(false, true, AudioType.OGGVORBIS);
}
}
}
}
}
Answer by rutter · May 15, 2012 at 10:38 PM
Directory.GetCurrentDirectory()
Looks like you're only reading the one directory. Browsing around folders will get more complicated -- among other things, you'll need to deal with path strings, folder and file permissions, and UI to represent all of these things -- but it should be possible on most platforms.
You'll probably want to spend some time reading up on .NET's System.IO
namespace, specifically including the Path
, File
, and Directory
classes.
I'm only concerned with reading from one directory. I thought appending "\\$$anonymous$$usic\\" to the current directory would work since it seems to be giving me the correct file names. I just don't know why it actually reads the data from the exe's directory.
I'll look more into the System.IO namespace. Thanks!
I just fixed the problem. By using an array of strings ins$$anonymous$$d of FileInfo, it just worked.
Answer by MMFTM · Jun 13, 2012 at 12:12 PM
Hi Wraxul,
(this isn't an answer, more of a request)
Am i right in saying that your program allows users to loads/access MP3 files from a folder other than the folders created during the PC Standalone player build? if so, im looking for just this kind of thing for a project im working on at the moment. im not code savvy but would love to be able to use your program in my project to enable people to load/access their own music, MP3s that is. i would much appriciate being able to do this.
Thanks for any feedback you might have.
Answer by Yarashiel · Oct 23, 2012 at 01:12 PM
Hi Wraxul ! I'm new to Unity 3D and have basic knowledge/experience in scripting.
Since I've discover the engine, I've wondered if the exact question of your topic was possible (so I don't create another answer). Sum-up : During application runtime, via the user interface, the user can load a file from his disk, which will somehow be executed then...
You've said that you fixed the problem by "using an array of strings instead of FileInfo". Could you be more specific about that? Or even show what the code would look like ?
I'd be very grateful if you'd take time to explain your process and logic ! Could you also upload some pictures of the result of your work ?!
Thanks by advance !
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Import a prefab from file at runtime? 1 Answer
Need to load prefabs during runtime 1 Answer
How to set a texture resource to texture type "GUI" by script? 1 Answer