Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Help Wanted How to load MP3 file into an AudioClip and make it stay compressed in memory?

Discussion in 'Audio & Video' started by ivoras, Aug 30, 2020.

  1. ivoras

    ivoras

    Joined:
    May 21, 2020
    Posts:
    13
    I've searched around, and can't find a solution for this.

    Basically, I have a multimedia / entertainment app, not really a game. It loads MP3 files from a server - but that doesn't matter so much because it stores them in the file system locally.

    When I use
    UnityWebRequestMultimedia.GetAudioClip($"file://{cacheFileName}", AudioType.MPEG)
    , it decompresses the MP3 file in memory, and since those are hour long MP3s, it results in hundreds of MB of memory taken just for the audio. I'm looking for a way to load MP3s from files so that they are either streaming or compressed in memory, at runtime, from a script.

    Importing MP3s within the editor isn't an option.
     
    Last edited: Aug 31, 2020
  2. ivoras

    ivoras

    Joined:
    May 21, 2020
    Posts:
    13
    Answering my own question in case someone encounters the same issue, the solution is in using
    DownloadHandlerAudioClip
    manually.

    Code (CSharp):
    1.             var dh = new DownloadHandlerAudioClip($"file://{cacheFileName}", AudioType.MPEG);
    2.             dh.compressed = true; // This
    3.  
    4.             using (UnityWebRequest wr = new UnityWebRequest($"file://{cacheFileName}", "GET", dh, null)) {
    5.                 yield return wr.SendWebRequest();
    6.                 if (wr.responseCode == 200) {
    7.                     audioSource.clip = dh.audioClip;
    8.                 }
    9.             }
    10.  
     
  3. colinleet

    colinleet

    Joined:
    Nov 20, 2019
    Posts:
    23
    1) Using file download handlers is far far far slower than using the System.File library in my experience... Is they any way to use this internal audio conversation without the file http overhead?

    2) Is there any way of controlling what bit depth of mp3 is loaded as (like covering it to 32 bit PMC instead of 16 bit) when the mp3 is decoded?

    3) Have you found any platform restrictions where this awesome method doesn't work? / If not I'm so happy they finally add a universal mp3 decoder now that the patent is expired.
     
unityunity