Async / Streaming Audio with UnityWebRequest

I’m trying to load an audioclip from a server.
In the past I’ve been using the WWW class GetAudioClip with the streaming boolean set to true.
With Unity’s UnityWebRequestMultimedia.GetAudioClip() streaming doesn’t seem to be an option.
Is there any other way other to load the audio data asynchronously or streaming it?

I kind of don’t want to revert back to using WWW because of its memory footprint.
But UnityWebRequestMultiMedia.GetAudioClip gives a 1000ms+ lagspike as it is not getting the audioclip async…
AssetBundle is not an option, can’t wait for a server to build asset bundles after mp3’s have been uploaded to the server it is required to be available immediately.

1 Like

Did you ever manage to do this?

nope, audioclip streaming from web is just not possible at this point (using 2017.2.0p3)
I ended up waiting until the audioclip is fully downloaded and then play the clip. Requiring the audio file to be lightweight.

I figured it out! Unity - Scripting API: Networking.DownloadHandlerAudioClip.streamAudio

This code works for me, hope it helps someone else :slight_smile:

private IEnumerator Start()
{
    using (var webRequest = UnityWebRequestMultimedia.GetAudioClip(WavPath, AudioType.WAV))
    {
        ((DownloadHandlerAudioClip)webRequest.downloadHandler).streamAudio = true;

        webRequest.SendWebRequest();
        while (!webRequest.isNetworkError && webRequest.downloadedBytes < 1024)
            yield return null;

        if (webRequest.isNetworkError)
        {
            Debug.LogError(webRequest.error);
            yield break;
        }
       
        var clip = ((DownloadHandlerAudioClip)webRequest.downloadHandler).audioClip;
        Source.clip = clip;
        Source.Play();
    }
}
3 Likes

Yeah the streaming feature got introduced in Unity 2018.2
Took them some time to implement this.
It is still tricky with different platforms though. Only recently I tried this in WebGL which does the job if mp3 is used. But in the editor it complains about not being supported (even with streamAudio false which is default). It still complains streaming of mp3 is not supported. This is due to some licensing which is just plain stupid that such a restriction exists.

1 Like

I ended up waiting until the audioclip is fully downloaded and then play the clip. Requiring the audio file to be lightweight.

Warning … if, like me, you want to download the whole sound you should change the lines :

        webRequest.SendWebRequest();
        while (!webRequest.isNetworkError && webRequest.downloadedBytes < 1024)
            yield return null;

to …

        yield return webRequest.SendWebRequest();

Well yeah that’s downloading the whole file. You’re not streaming anymore then. Which means you have to wait until the whole file is downloaded before being able to play. The larger the file, the longer the wait.

I just wish there was some handling done by unity to safely stream the audio but instead you have to keep an eye on download speed and file size if you want to stream without buffer interruptions.

1 Like

Yeah, it’s kind of frustrating that the API for this is so janky. There should be a built-in function that returns an AudioClip called GetStreamingAudioFromURI() that handles everything for you.

2 Likes