Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • 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 ConsoleLog · Mar 23 at 05:11 PM · audiowwwaudiosourceaudioclip

Get AudioClip from Path?,How to get AudioClip from a path?

Hiya. I am working on a project in Unity, and I've gotten stuck. I want the player to be able to select an audio file from their PC and have my program add it to a playlist to be listened to. I have a functional Playlist, and I can create space for a new song and open the explorer to let the player choose a file, but I can't get program to add the AudioClip to the playlist.

 public AudioClip[] currentPlaylist;
 string path;
 public AudioClip newSong;
 public void OpenExplorer()
     {
         path = EditorUtility.OpenFilePanel("Add Song", "", "mp3");
         GetSong();
     }
  void GetSong()
     {
         if (path != null)
         {
             UpdatePlaylist();
         }
     }
  void UpdatePlaylist()
     {
 
         WWW www = new WWW("file:///" + path);
         newSong = www.GetAudioClip();
         Array.Resize(ref currentPlaylist, (currentPlaylist.Length+1));
 
         currentPlaylist[currentPlaylist.Length-1] = newSong;
     }

I'm scratching my head at what isn't working, and I'm not sure if I need to make a fix to a bug here or if I just need to use a totally new approach. Sorry if this is a really simple question.

Comment
Add comment · Show 2
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 Hellium · Mar 23 at 05:25 PM 0
Share

Very important note is that EditorUtility does not work in a built game, as the name suggests, it's an editor only function.

avatar image ConsoleLog Hellium · Mar 23 at 05:33 PM 0
Share

Thank you very much for noting that, I will keep that in mind and update my method of selecting a file accordingly.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by NikkF · Mar 23 at 10:49 PM

Here's the code I use when getting audio files from the machine. In this example, I save all of the clips to a list using public List<AudioClip> clips; and call the method by calling LoadAudio(filename);

Filename is the name of the actual file ("song.mp3") and the path is something like "C:\Users\User\Desktop".

Here's the code for getting the file and adding it to the list;

     private IEnumerator LoadAudio(string name)
     {
         WWW request = GetAudioFromFile(soundPath, name);
         yield return request;
         audioClip = request.GetAudioClip();
         audioClip.name = name;
         clips.Add(audioClip);
     }
 
     private WWW GetAudioFromFile(string path, string filename)
     {
         string audioToLoad = string.Format(path + "{0}", filename);
         WWW request = new WWW(audioToLoad);
         return request;
     }



If you have any problems or questions, let me know.
Hope this helps!

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.

Follow this Question

Answers Answers and Comments

175 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 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 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 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 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 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 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 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 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 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

How do i stream music from a server? 0 Answers

WWW Audio and PlayOneShot: Playing the same AudioClip Twice Cuts off First Instance 1 Answer

Problem with playing sound on Trigger-Enter 2 Answers

How to get decibel's value of game's sound? 0 Answers

Play audio at consistent volume 2 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