• 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
1
Question by fluxhackspro · Jun 15, 2018 at 05:00 PM · wwwloadaudioclip

Load audioclip from folder on computer into game, in runtime

Hello I want the player to specify the music in the game, and for that i need to load it somehow from a folder on the computer. Specificly i want to load all audioclips of mp3 from a specific folder. Probably the same as the game folder, then import them into the game somehow. I've been researching a bit and found this: https://docs.unity3d.com/ScriptReference/WWW.GetAudioClip.html

But how does one use this correctly and actually set some kind of prefix to the correct folder?

UPDATE:

Unity doesn't support mp3 it looks like so i converted to .wav I found a post on unity forums and here is my code. Note that his still doesnt give me anything, i dont get an error but the debug message is empty so it doesnt find the file correctly?

Any help is welcome

     public AudioClip[] clips;
     private string musicDir = "C://Users//Ägaren//Documents//FluxClips";
     AudioSource source;
 
     void Awake ()
     {
         source = GetComponent<AudioSource> ();
     }
 
     void Start ()
     {
         //AudioClip selectedClip = clips [Random.Range (0, clips.Length)];
         WWW audioLoader = new WWW ("file://" + musicDir + "//clip.wav");
         AudioClip selectedClip = audioLoader.GetAudioClip (false, false, AudioType.WAV);
         source.clip = selectedClip;
         Debug.Log (source.clip.name);
     }
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
3

Answer by neoRiley · Mar 13, 2020 at 04:52 PM

@fluxhackspro - thanks for the reminder on how to load from the file system - I thought I'd throw this out as a possible answer as well with the updated UnityWebRequest/UnityWebRequestMultimedia object with an async example:

 async void Start()
 {
     // build your absolute path
     var path = Path.Combine(Application.dataPath, "Audio", "sounds", "myAudioClip.wav");
     
     // wait for the load and set your property
     CurrentClip = await LoadClip(path);
 
     //... do something with it
 }
 
 async Task<AudioClip> LoadClip(string path)
 {
     AudioClip clip = null;
     using (UnityWebRequest uwr = UnityWebRequestMultimedia.GetAudioClip(path, AudioType.WAV))
     {
         uwr.SendWebRequest();
 
         // wrap tasks in try/catch, otherwise it'll fail silently
         try
         {
             while (!uwr.isDone) await Task.Delay(5);
 
             if (uwr.isNetworkError || uwr.isHttpError) Debug.Log($"{uwr.error}");
             else
             {
                 clip = DownloadHandlerAudioClip.GetContent(uwr);
             }
         }
         catch (Exception err)
         {
             Debug.Log($"{err.Message}, {err.StackTrace}");
         }
     }
 
     return clip;
 }


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 mgstauff · Nov 15, 2020 at 07:41 PM

For runtime loading of audiofiles from filesystem you can use AudioImporter: https://github.com/Hello-Meow/AudioImporter

Use the bass audio library plugin to support more formats.

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 Flavosky · Feb 08 at 10:29 PM

Hello everyone,

I would like to add Audio Importer to a Game Object and when I right click on it, that opens the Audio Importer Browser. An idea ?

(I am Sound Designer and C# is pretty difficult to me so.. :/)

Thanks a lot !

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

91 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

Related Questions

Can you load an AudioClip from the file system w/o using WWW? 0 Answers

Webplayer crash on LoadImageIntoTexture call 1 Answer

How to get audioclip from UnityWebRequest 1 Answer

Is it possible to load a script from a url? 1 Answer

the texture appear rubbish 1 Answer

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