• 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
Question by TheJordan · May 02, 2013 at 10:44 PM · androidaudioclipfilebrowseraudioclip www load

Can I replace an audio clip with a user selected file on android?

I wrote a music visualizer using songs just dragged into my unity project to test it. Now, I want to replace that with a menu for users to select songs from their library. So far I am using javascript. Please point me in the right direction.

Comment
Hazzanger

People who like this

1 Show 0
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
Best Answer

Answer by TheJordan · May 09, 2013 at 09:49 AM

Alright. To load an external music file and put in in an audioSource at runtime (with javascript), you need a combination of "System.IO" and "WWW".

Here is a stripped down version of how I solved it for my project. I have not tested this exact code, but it should work if you put an audiosource and this script on a game object.

This should pick the first song alphabetically in the music folder on an android device and play it. There is no error handling so an absent music folder will cause issues like no song being played.

 var MusicFolder : System.IO.DirectoryInfo;
 var myClip : WWW;
 var myPath : String;
 
 function Start()
 {
     myPath = "/mnt/sdcard/music";
     MusicFolder = new System.IO.DirectoryInfo(myPath);
     myClip = new WWW("file:///" + MusicFolder.GetFiles()[0].FullName);
     audio.clip = myClip.GetAudioClip(false, false);
 }
 function Update()
 {
     if (!audio.isPlaying && audio.clip.isReadyToPlay){
         soundPlayer.audio.Play();
     }
 }
Comment
NeightLocke
Hazzanger
QuDi
migue_jr

People who like this

4 Show 6 · 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 kUr4m4 · Jan 13, 2014 at 11:13 AM 0
Share

Have you reached a solution for this yet?

avatar image LaurensiusTony · Jun 18, 2014 at 04:48 AM 0
Share

hi can you translate this to windows phone?

avatar image bugie · Aug 24, 2014 at 03:18 PM 0
Share

Hi, I have tested out this solution and the path does not work. I have tried using a path for my PC and it works perfectly. The android path it is translating it to C:\mnt\sdcard\music. Did you use a different path or add anything to that code to make it work? Would appreciate an answer. Thanks a lot

avatar image Tribe · Jan 08, 2015 at 02:06 PM 0
Share

I have also tested this solution right now and it works, maybe you forgot to give READ_EXTERNAL_STORAGE-permission. The code snippet works except there is no audio nor soundPlayer declared.

avatar image gaberutd Tribe · Jul 08, 2016 at 10:14 PM 0
Share

What data types do the soundPlayer and audio variables need to be declared as?

avatar image Lucifer95 · Jan 15, 2015 at 04:56 PM 0
Share

"/mnt/sdcard/music"; is convered to c:\mnt\sdcard\music and hence i cannot load files from android...works perfectly on pc any solution please??

avatar image

Answer by DaveA · May 02, 2013 at 11:06 PM

I think it's Prime31 that has an Android library for accessing pictures from the phone's library. Maybe it could be used as a template for accessing music too.

Comment

People who like this

0 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 TheJordan · May 03, 2013 at 02:39 AM 0
Share

Thanks. I'd rather program it myself than pay someone else for a plugin though. I'll post the answer here when I figure it out for all you people from Google in the future.

avatar image CroHack97 TheJordan · Sep 18, 2015 at 09:38 PM 0
Share

Hey, I know it past few years since you posted this, just to ask did you found way to select music from mobile that plays in unity audiosource?

avatar image

Answer by QuDi · Jul 31, 2018 at 09:22 PM

Here is C# version(now only PC version):

     private System.IO.DirectoryInfo MusicFolder;
      private WWW myClip;
      public string myPath;
     private AudioSource audioSource;
     
     void Start () {
         audioSource = GetComponent<AudioSource>();
         MusicFolder = new System.IO.DirectoryInfo(myPath);
         myClip = new WWW("file:///" + MusicFolder.GetFiles()[0].FullName);
         audioSource.clip = myClip.GetAudioClip(false, false);
     }
     
     
     void Update () {
         if (!audioSource.isPlaying && audioSource.clip.isReadyToPlay){
              audioSource.Play();
          }
     }

In future planing to make it useful for mobile devices

Comment

People who like this

0 Show 0 · 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

17 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

Related Questions

loading/streaming audioclip using www class [android] 0 Answers

How can i create an AudioClip from byte array? 0 Answers

AudioClip huge lag on some android devices 0 Answers

Is it possible to save an audioclip to android internal storage? 0 Answers

IOS Load AudioClip from local 0 Answers


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