Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Audio Async / Streaming Audio with UnityWebRequest

Discussion in 'Audio & Video' started by MaskedMouse, Aug 7, 2017.

  1. MaskedMouse

    MaskedMouse

    Joined:
    Jul 8, 2014
    Posts:
    1,058
    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.
     
    Last edited: Aug 7, 2017
    cxode likes this.
  2. nilsdr

    nilsdr

    Joined:
    Oct 24, 2017
    Posts:
    374
    Did you ever manage to do this?
     
  3. MaskedMouse

    MaskedMouse

    Joined:
    Jul 8, 2014
    Posts:
    1,058
    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.
     
  4. cxode

    cxode

    Joined:
    Jun 7, 2017
    Posts:
    268
  5. cxode

    cxode

    Joined:
    Jun 7, 2017
    Posts:
    268
    This code works for me, hope it helps someone else :)

    Code (CSharp):
    1. private IEnumerator Start()
    2. {
    3.     using (var webRequest = UnityWebRequestMultimedia.GetAudioClip(WavPath, AudioType.WAV))
    4.     {
    5.         ((DownloadHandlerAudioClip)webRequest.downloadHandler).streamAudio = true;
    6.  
    7.         webRequest.SendWebRequest();
    8.         while (!webRequest.isNetworkError && webRequest.downloadedBytes < 1024)
    9.             yield return null;
    10.  
    11.         if (webRequest.isNetworkError)
    12.         {
    13.             Debug.LogError(webRequest.error);
    14.             yield break;
    15.         }
    16.        
    17.         var clip = ((DownloadHandlerAudioClip)webRequest.downloadHandler).audioClip;
    18.         Source.clip = clip;
    19.         Source.Play();
    20.     }
    21. }
    22.  
     
    andrew-lukasik, Dance_M and jesper42 like this.
  6. MaskedMouse

    MaskedMouse

    Joined:
    Jul 8, 2014
    Posts:
    1,058
    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.
     
    cxode likes this.
  7. ComplexSkunk

    ComplexSkunk

    Joined:
    Feb 29, 2020
    Posts:
    1
    I ended up waiting until the audioclip is fully downloaded and then play the clip. Requiring the audio file to be lightweight.
     
  8. jesper42

    jesper42

    Joined:
    Jan 11, 2011
    Posts:
    28
    Warning ... if, like me, you want to download the whole sound you should change the lines :

    Code (CSharp):
    1.  
    2.         webRequest.SendWebRequest();
    3.         while (!webRequest.isNetworkError && webRequest.downloadedBytes < 1024)
    4.             yield return null;
    5.  
    to ...

    Code (CSharp):
    1.  
    2.         yield return webRequest.SendWebRequest();
    3.  
     
  9. MaskedMouse

    MaskedMouse

    Joined:
    Jul 8, 2014
    Posts:
    1,058
    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.
     
    cxode likes this.
  10. cxode

    cxode

    Joined:
    Jun 7, 2017
    Posts:
    268
    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.