#Load audio file to AudioSource

1 messages · Page 1 of 1 (latest)

slim socket
#

Hi, I am struggling to find a solution for this problem. I am downloading mp3 file from internet, saving it locally and I want to put it into an AudioSource clip. But it is not working for me. Clip just becomes blank field in inspector and no sound is being played. Here's my code:

    IEnumerator LoadAudioClip(string filePath)
    {
        Debug.Log("file://" + filePath);
        using (UnityWebRequest www = UnityWebRequestMultimedia.GetAudioClip("file://" + filePath, AudioType.MPEG))
        {
            yield return www.SendWebRequest();

            if (www.isNetworkError || www.isHttpError)
            {
                Debug.LogError(www.error);
            }
            else
            {
                audioSource.clip = DownloadHandlerAudioClip.GetContent(www);
                audioSource.Play();
            }
        }
    }

I could just use UnityWebRequestMultimedia.GetAudioClip straight from the internet, without saving the file locally I suppose, but I wasn't able to get it working. I was getting 400 and 422 response codes, because I also need to send body with the post request. But now I got code that downloads the file without Unity libraries.

nimble wedge
#

I would recommend downloading it and saving it in your project as a .wav file because that’s normally what Unity uses. That way, you can take all of the AudioClips that you need from the project window and swap them out when you need to using AudioSouce.clip