Search Unity

Question 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:
    66
    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:
    66
    Answering my own question in case someone encounters the same issue, the solution is in using
    DownloadHandlerAudioClip
    manually.

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

    colinleet

    Joined:
    Nov 20, 2019
    Posts:
    189
    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.
     
  4. rwbennet

    rwbennet

    Joined:
    Feb 19, 2014
    Posts:
    2
    omg this. Loading mp3's dynamically from the internet into a webgl build was proving to be completely unusable until I found this. Thanks so much.
     
  5. sayatraykulov

    sayatraykulov

    Joined:
    May 21, 2022
    Posts:
    1
    Thanks bro. How much time did i spend to find your solution. Its so disappointing that unity still not able to load mp3 files from local disk storage. In my case i downloaded base64 data of mp3 and did not find any helpfull ideas to play that mp3 data untill i read your solution.
     
  6. I5

    I5

    Joined:
    Feb 15, 2015
    Posts:
    21
    sweeeet! Thanks for putting out this solution.
     
  7. MaxLohMusic

    MaxLohMusic

    Joined:
    Jan 17, 2022
    Posts:
    67