Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Load audio file from disk fails only on OSX

Discussion in 'Audio & Video' started by Doddler, Nov 12, 2023.

  1. Doddler

    Doddler

    Joined:
    Jul 12, 2011
    Posts:
    268
    So I have a rather unusual situation, I'm trying to get my game to work on OSX, but here's the problem: I don't personally have a mac to test this on. I'm simply building the game and sending it to a friend, and I am generally stuck waiting hours or days between creating a build and getting a test result. No matter what I do however, I can't make it load the audio file from disk no matter what I try.

    Here's what I'm doing:

    1) The path I am using is created via Path.Combine(Application.streamingAssetsPath, filename);
    2) File.Exists(path); returns TRUE for the path. The path is guaranteed to be valid.
    3) UnityWebRequestMultimedia.GetAudioClip(path) FAILS: invalid uri exception.
    4) using "file:///" + path FAILS, file not found exception.
    5) using "file://" + path FAILS, file not found exception. (only 2 slashes, because the full path will already start with a /)
    6) using "file:///" + UnityWebRequest.EscapeURL(path); FAILS: invalid uri exception. This also fails for 2 slashes.
    7) Copying the path from any of the steps 4-6 into the browser plays the file.
    8) The path generated by step 5 and 6 work on windows AND linux platforms.

    I have no idea why this would fail, and I'm at wits end. To reiterate, File.Exists returns true for my original path, it is a valid file, but I cannot get unity to load this file. This works on windows and linux. As best as I know, the only way to load an audio file from disk is with the web request, there is no built in method to load a file from disk without it. What's worse is I'm pretty sure this used to work, as I've had this working on osx before.

    Does anyone know what I'm doing wrong?
     
    Last edited: Nov 12, 2023
  2. Doddler

    Doddler

    Joined:
    Jul 12, 2011
    Posts:
    268
    Welp, I figured it out, and (shocking) it's because unity is doing something wrong.

    Code (CSharp):
    1. var baseUri = new Uri(path);
    2. var req = UnityWebRequestMultimedia.GetAudioClip(baseUri.AbsoluteUri, UnityEngine.AudioType.OGGVORBIS);
    3. yield return req.SendWebRequest();
    4. audioClip = DownloadHandlerAudioClip.GetContent(req);
    This is what I've learned is the recommended code to load a local audio file from the OS, for any desktop platform. Then why did it fail for me before? Well, it's because I had a + in the path. Unity will fail to load a local file that has + in the path, even though + is valid in a local file Uri, and even the c# uri helper will output a uri with a + in the path. This would have failed on windows as well, but unluckily only my mac build had the + in the path. For reasons I don't understand, only the + seems to cause issues, spaces are no problem.

    I can only imagine why this occurs, but I suspect unity is probably decoding the + as a space. According to specs though, spaces should only be encoded as a + when submitting an
    application/x-www-form-urlencoded
    string on a web form, so unity is wrong in this behavior.

    The final code that works on all platforms is as follows:
    Code (CSharp):
    1. var baseUri = new Uri(path);
    2. var req = UnityWebRequestMultimedia.GetAudioClip(baseUri.AbsoluteUri.Replace("+", "%2b"), UnityEngine.AudioType.OGGVORBIS);
    3. yield return req.SendWebRequest();
    4. audioClip = DownloadHandlerAudioClip.GetContent(req);
    Unity does have a function to url escape a string, but sadly for me it will also escape the slashes, meaning the only reliable way to not mangle my uri is string.replace. It's not great, but neither is creating a remote web request for a local file as unity does not expose any other way.
     
    Last edited: Nov 12, 2023
  3. SeventhString

    SeventhString

    Unity Technologies

    Joined:
    Jan 12, 2023
    Posts:
    380
    Wow sorry about that.... :confused:

    I'm glad (and grateful!) that you found (and shared!) the solution, I'll flag this internally.

    Thanks!
     
  4. Doddler

    Doddler

    Joined:
    Jul 12, 2011
    Posts:
    268
    Thanks for your reply, I had filed a bug report on this and am happy to see that it was confirmed as a proper bug. :)