Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Load mp3 from user file at runtime(windows)?

Discussion in 'Audio & Video' started by Shack_Man, Nov 27, 2018.

  1. Shack_Man

    Shack_Man

    Joined:
    Jun 7, 2017
    Posts:
    365
    I'm trying to build an app that can play any song that the user drags into a specific folder. To my understanding asset budles don't work because they have to be prepared and specified beforehand. I've found solutions where people load it from the web, but not how to load whatever is in that folder.
     
  2. Hosnkobf

    Hosnkobf

    Joined:
    Aug 23, 2016
    Posts:
    1,076
    You can use the same logic as loading from the web. As URL you have to pass
    "file://" + path
    .
     
    Shack_Man likes this.
  3. Shack_Man

    Shack_Man

    Joined:
    Jun 7, 2017
    Posts:
    365
    Thanks, using WWW to get files from the local hard drive didn't seem intuitive to me.. Found this nice article and got it working now...at least until hitting the brick wall of not being able to play mp3 on windows, but ogg is fine as well.

    https://www.raywenderlich.com/479-using-streaming-assets-in-unity


    Code (CSharp):
    1.  private List<AudioClip> songs;
    2.  
    3.  
    4.     private void GetSongsFromFolder()
    5.     {
    6.         DirectoryInfo directoryInfo = new DirectoryInfo(Application.streamingAssetsPath);
    7.         FileInfo[] songFiles = directoryInfo.GetFiles("*.*");
    8.  
    9.         foreach (FileInfo songFile in songFiles)
    10.         {
    11.            StartCoroutine(ConvertFilesToAudioClip(songFile));
    12.         }
    13.        
    14.     }
    15.  
    16.     private IEnumerator ConvertFilesToAudioClip(FileInfo songFile)
    17.     {
    18.         if(songFile.Name.Contains("meta"))
    19.             yield break;
    20.         else
    21.         {
    22.             string songName = songFile.FullName.ToString();
    23.             string url = string.Format("file://{0}", songName);
    24.             WWW www = new WWW(url);
    25.             yield return www;
    26.             songs.Add(www.GetAudioClip(false,false));
    27.         }
    28.     }
    .
     
  4. Hosnkobf

    Hosnkobf

    Joined:
    Aug 23, 2016
    Posts:
    1,076
    The WWW class is the old way which is recommended not to use anymore. Instead you should use UnityWebRequest like this:
    Code (CSharp):
    1. using (UnityWebRequest web = UnityWebRequestMultimedia.GetAudioClip(url))
    2. {
    3.     yield return web.SendWebRequest();
    4.     if(!web.isNetworkError && !web.isHttpError)
    5.     {
    6.         var clip = DownloadHandlerAudioClip.GetContent(web);
    7.         if(clip != null)
    8.         {
    9.             songs.Add(clip);
    10.         }
    11.     }
    12. }
     
    Shack_Man likes this.
  5. cecarlsen

    cecarlsen

    Joined:
    Jun 30, 2006
    Posts:
    848
    Only WAV files works for me (locally using "file://"). Has anyone succeeded with ogg files?
     
  6. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,637
    How do you load ogg files?
    Did you specify the audio type?
     
  7. Shack_Man

    Shack_Man

    Joined:
    Jun 7, 2017
    Posts:
    365
    The script I posted above works with ogg (on windows) for me.

    I was actually curios, once I imported the files, is there a way to save it? Right now everytime I start the app it has to load in the ogg files and freezes for a bit if there are more than 5 songs.
     
  8. DhiaSendi

    DhiaSendi

    Joined:
    May 16, 2018
    Posts:
    42
    To make it works with MP3 files make sure to add AudioType.MPEG
    Code (CSharp):
    1. IEnumerator GetAudioClip(string fileName,List<AudioClip> audioClips)
    2.     {
    3.         UnityWebRequest webRequest = UnityWebRequestMultimedia.GetAudioClip(
    4.             filePath+"/"+fileName,AudioType.MPEG);
    5.          
    6.         yield return webRequest.SendWebRequest();
    7.  
    8.         if(webRequest.isNetworkError)
    9.         {
    10.             Debug.Log(webRequest.error);
    11.         }
    12.         else
    13.         {
    14.             AudioClip clip = DownloadHandlerAudioClip.GetContent(webRequest);
    15.             clip.name = fileName;
    16.             audioClips.Add(clip);
    17.  
    18.         }
    19.     }