Downloaded audio fails to play

Hi. I’m new to Unity and looking for some help.

I am saving system.io.stream as a mp3 file and trying to play the audio right after. The code runs fine with no error audio does not play. I think it is being triggered to play before the download is complete / object is created. I’m able to run it fine with a file that already exist.

using (var fileStream = File.Create(audioPath))
                    {
                        audioAPIResponse.AudioStream.CopyTo(fileStream);
                        fileStream.Flush();
                        fileStream.Close();
                        var playClip = Resources.Load<AudioClip>(filename);
                        SoundManager.Instance.PlaySound(playClip);
                    }

audioPath = Resources/<epoch_time>.mp3
filename = <epoch_time>

SoundManager script

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SoundManager : MonoBehaviour
{
    public static SoundManager Instance;
    [SerializeField] private AudioSource _musicSource;

    void Awake() {
        if (Instance == null) {
            Instance = this;
            DontDestroyOnLoad(gameObject);
        }
        else {
            Destroy(gameObject);
        }
    }

    public void PlaySound(AudioClip clip) {
        _musicSource.PlayOneShot(clip);
    }
}

What am I doing wrong? Any way to improve this flow? Maybe not save as file but play the stream and discard?

Sorry for the long delay. Everything coming through Resource.Load has to be imported in the Unity Editor and then “built into” in the final game. But you can still achieve what you need by using UnityWebRequestMultimedia.GetAudioClip. Here is an example script that plays a file at runtime.

using System.Collections;
using UnityEngine;
using UnityEngine.Networking;

public class LoadRuntimeAudio : MonoBehaviour
{
    public string path;     //Absolute path like C:\Users\Name\Desktop\music.mp3
    public AudioType type;

    public AudioSource source;

    void Start()
    {
        StartCoroutine(GetAndPlayAudioClip());
    }

    IEnumerator GetAndPlayAudioClip()
    {
        using (var uwr = UnityWebRequestMultimedia.GetAudioClip(path, type))
        {
            ((DownloadHandlerAudioClip)uwr.downloadHandler).streamAudio = true;

            yield return uwr.SendWebRequest();

            if (uwr.isNetworkError || uwr.isHttpError)
            {
                Debug.LogError(uwr.error);
                yield break;
            }

            AudioClip myClip = DownloadHandlerAudioClip.GetContent(uwr);

            source.clip = myClip;
            source.Play();
        }
    }
}

I want to download ringtone and use it
I can’t hear it when I download it and I have a ringtone set on my phone
what is that error?