• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by wraxul · May 15, 2012 at 10:00 PM · c#runtimeloadmusicdirectory

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);
                 }
             }    
         }
     }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image wraxul · May 16, 2012 at 12:37 AM 0
Share

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!

avatar image wraxul · May 16, 2012 at 12:56 AM 0
Share

I just fixed the problem. By using an array of strings ins$$anonymous$$d of FileInfo, it just worked.

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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 !

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Need to load prefabs during runtime 1 Answer

Multiple Cars not working 1 Answer

Import a prefab from file at runtime? 1 Answer

Distribute terrain in zones 3 Answers

Getting Debug log errors when I load my scenes 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges