Home > other >  Unity, MacOS, cannot load audio from StreamingAssets using www.SendWebRequest()
Unity, MacOS, cannot load audio from StreamingAssets using www.SendWebRequest()

Time:01-12

I am working on an app targeting Windows, built in Unity. All the audio files are in streamingAssets so the client can replace them and customize the app. I have the following code loading the audio files into Audio Sources and it's working fine on Windows.

void SetAudioFromStreamingAssets()
{
    if (string.IsNullOrEmpty(StreamingAssetFilePath)) return;
    string fullpath = Application.streamingAssetsPath   "/"   StreamingAssetFilePath;
    if (!File.Exists(fullpath)) return;

    StartCoroutine(SetAudioClipFromFile(source,fullpath));
}

IEnumerator SetAudioClipFromFile(AudioSource audioSource, string path)
{
    using (UnityWebRequest www = UnityWebRequestMultimedia.GetAudioClip(path, AudioType.UNKNOWN))
    {
        yield return www.SendWebRequest();
        if (www.isNetworkError)
        {
            VisDebug.AppendTxt("ERROR. Could not load music file: "   www.error, true);
        }
        else
        {
            AudioClip myClip = DownloadHandlerAudioClip.GetContent(www);
            audioSource.clip = myClip;
        }
    }
}

A colleage of mine is trying to work on the project but he only has a mac. And the audio is not loading on his mac. The code is reaching this line..

VisDebug.AppendTxt("ERROR. Could not load music file: "   www.error, true);

So File.Exists is true, and also www.isNetworkError is true. and the www.error is...

Cannot connect to destination host

EDIT

The answer below is correct and just fyi here is an example of what the path needs to look like on macos.

file:///Users/spoopy/Documents/GitHub/Thing/Assets/StreamingAssets/GameLoad.wav

CodePudding user response:

You have to prefix your path with file:// on MacOS and Linux.

You could put something like this after your variable definition.

#if UNITY_STANDALONE_OSX || UNITY_EDITOR_OSX
    fullpath ="file://"   path);
#endif

Source

  • Related