Hello, i have a problem over here and cant figure out, how to make a streaming audio download from server. I mean, there is an audio file on server, and i need not to download and play it, but play it while it
s downloading and save for the runtime(not to cache it or save somewhere on disk). For both PC and mobile platform. For now, i can only download and play it. Maybe there is a solution with creating audio from downloaded bytes? Code below:
public class StreamingAudio:MonoBehaviour
{
public AudioSource audioSource;
public string URL;
public void Start()
{
StartCoroutine(GetData());
}
public IEnumerator GetData()
{
// Creating download handler
DownloadHandlerAudioClip downloadHandler = new DownloadHandlerAudioClip("", AudioType.OGGVORBIS);
//Make audio streaming
downloadHandler.streamAudio = true;
UnityWebRequest request = new UnityWebRequest(URL, "GET", downloadHandler, null);
// downloading audio file
request.SendWebRequest();
AudioClip audioClip = null;
//checking is there is clip
while (audioClip == null )
{
try
{
//trying to recieve audio
audioClip = DownloadHandlerAudioClip.GetContent(request);
}
catch (Exception e) {
//returns message that audio download not finished
Debug.Log("Exception: " + e.Message);
}
//checking downloadedBytes size (maybe i can do smthg with these bytes?)
Debug.Log("Загружено байтов: "+ request.downloadedBytes );
yield return 0f;
}
// playing audio source on download finished
audioSource.clip = audioClip;
audioSource.Play();
yield return null;
}
}