• 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 iRaMb0 · Jul 28, 2013 at 06:41 PM · audiofiles

(Improved)FileBrowser Audio

So i want the player to be able to choose music from his HardDrive to play during runtime. I saw that there is an (Improved)File Browser but i'm not sure how to set it up.I tried setting it up but i was confused.

1.The browser consists of 3 C# scripts?

2.Where do i put those scripts?

3.Can i make it so the player chooses audio from his HardDrive?

I'm not familiar with C# so this is kinda hard for me :D Thanks.

(Link to the Browser : http://wiki.unity3d.com/index.php?title=ImprovedFileBrowser )

Comment
Add comment · Show 3
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 chummscrubber · Jul 29, 2013 at 04:19 AM 0
Share

I actually asked the same question a little while back, here is a link to $$anonymous$$e, vote up the question and answer if its what you were after.

http://gamedev.stackexchange.com/questions/59525/issues-loading-in-audiosource-at-runtime-using-www-class/59527?noredirect=1#comment104789_59527

avatar image iRaMb0 · Jul 29, 2013 at 02:06 PM 0
Share

Do i have to combine the "piecies" of code to make it work ?!By the way that is better than what i wanted :D

avatar image chummscrubber · Jul 29, 2013 at 11:23 PM 0
Share

so to get it to work you need the code sections from the Downloading to List section downwards. This will play any .ogg audio files in the specified directory.

This line is the most important and the only line you may want to change. Set the musicDir variable to whatever you want the base music directory, also you way want to set the SearchOption to Recursive to go through each subdirectory of the specified folder. string[] playlist = Directory.GetFiles(@musicDir, "*.ogg", SearchOption.TopDirectoryOnly);

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by chummscrubber · Jul 30, 2013 at 12:07 AM

Here is my code from the stack overflow question, basically all you need to do from here is define the audio directory to find the music in (which you would use the file browser for). In order to use this script simply attach it to an empty game object within your scene.

It is also worth noting that Unity only supports certain file formats for music (google this). In the case of this code I am only looking for .ogg music files, you way wish to change that, also you may want a recursive search to look for music in sub-directories (check the Directory.GetFiles() documentation for more on that).

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using System.IO;
 
 [RequireComponent(typeof(AudioSource))]
 public class MP3AudioImporter : MonoBehaviour {
     
     private List<AudioClip> audioClips;
     private const string musicDir = "Path/To/Your/Music/Directory";
         
     void Start()
     {
         audioClips = new List<AudioClip>();
         StartCoroutine("PlayAudioList");
     }
         
     IEnumerator DownloadPlaylist()
     {
         string[] playlist = Directory.GetFiles(@musicDir, "*.ogg", SearchOption.TopDirectoryOnly);
         
         foreach(string song in playlist)
         {
             WWW audioLoader = new WWW("file://" + song);
             
             while( !audioLoader.isDone )
                    yield return null;
             
             audioClips.Add(audioLoader.GetAudioClip(false));
         }    
     }
         
         
     IEnumerator PlayAudioList()
     {    
         yield return StartCoroutine("DownloadPlaylist");
 
         foreach(AudioClip song in audioClips)
         {
             audio.clip = song;
             audio.Play();
             yield return new WaitForSeconds(song.length + 1.0f);
         }
     }
 }
Comment
Add comment · Show 9 · 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 iRaMb0 · Jul 30, 2013 at 10:08 AM 0
Share

I think we're getting there but(!):

1.The problem is i don't know how to set up the file browser ( xD )

  1. get errors with your script

error CS0116: A namespace can only contain types and namespace declarations

I get this on (1,25),(2,16),(4,6),(10,13),(23,13)

I wish i knew C#, it would be so much easier :(

avatar image chummscrubber · Jul 30, 2013 at 01:20 PM 0
Share

Ok ive updated so you have my full code for audio, I would recommend using a hard coded $$anonymous$$usic directory path to begin with. Also you should go out and learn C# (or any object oriented language), it will come in handy for Unity. Writing out a full solution for you would kinda defeat the purpose of this space which is to get specific answers to problems (not full solutions)

avatar image iRaMb0 · Jul 30, 2013 at 02:31 PM 0
Share

Thanks a lot man, i appreciate it. I'll probably try to learn some C# :D

avatar image 1911_Dev · Aug 06, 2013 at 11:21 PM 0
Share

This works fine with .ogg and .wav but not with .mp3 (even changing filter nothing starts).What is wrong?

avatar image chummscrubber · Aug 07, 2013 at 07:15 AM 0
Share

I mentioned above that not all audio formats are supported by Unity ($$anonymous$$P3 is one of them) it can be imported by drag and drop into the project but this just converts the files to .ogg files for you. Check this for more info http://docs.unity3d.com/Documentation/$$anonymous$$anual/AudioFiles.html, if you can figure out how to leverage the Importer used to convert formats then you may be able to use $$anonymous$$P3 files by converting them to a unity readable format (though this may cause copyright problems for your game if you release). $$anonymous$$y advice is to find an online converter and convert $$anonymous$$P3 files this way, instruct potential buyers to do the same if they want music.

Show more comments
avatar image
0

Answer by gregzo · Jul 29, 2013 at 05:21 AM

Using the www class to load audio works with most formats, but does not allow GetData to be called on the created AudioClips. For your use, it should be fine - WWW.GetAudioClip.

Another solution, which I use to load wav samples into a sampler, is to parse the wav file, convert the int16s into floats, strip the header, create an AudioClip of the right size, and set the floats in it with SetData. It's a bit of work, but I've posted some of the steps on the Unity Forums already.

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

18 People are following this question.

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

Related Questions

[Android] Resources folder: number of files limit? 0 Answers

Is Unity applying effects to my audio files by default? 0 Answers

How to load files from persistentDataPath to memory? 1 Answer

Is it possible to play midi files in unity? 2 Answers

How to get a list of AudioClips from a directory at runtime? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges