Streaming audio with UnityWebRequest from server

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 its 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;
    }
}

Hi,

Get the same problem, Did you get a resolution on this?

Bye,

Jean

Have you found any solution to this and I would love to know the possible solution to it? Soon after you get it can you post the solution here.

Necro but in case others come across this post in search results, I’ve written an example how here:

I have not been able to get this to work either. I think the streaming flag is ignored. I tried Invertex’s example too but no dice.

I noticed that no matter what the downloadHandler.audioClip.loadType is DecompressOnLoad instead of Streaming.

Strange, are you loading the audio clip from local files or a web server? My example worked fine for me playing an MP3 from a web server.

What Unity version as well? I tested it on 2022.2.1

I’m streaming from a web server using 2021.3.x

For reference it’s this project:
https://github.com/RageAgainstThePixel/com.rest.elevenlabs

I really need this too. From a text to speech on my webserver. Does anyone figured it out?

I got this error using your code:
Couldn’t process audio stream.
UnityEngine.Debug:Log (object)

Maybe there’s something wrong with the streaming server?
here’s the URL for my server (test)

https://cortex-api.lingvo.id/audio_stream (it streamed perfectly on browser)

after messing with the bytes and correcting audio type, i got this error instead. Anyone having the same thing?