Search Unity

DownloadHandlerAudioClip GetContent crashes web audio when system is out of memory

Discussion in 'Web' started by rdpeake, Oct 23, 2018.

  1. rdpeake

    rdpeake

    Joined:
    Jan 26, 2017
    Posts:
    5
    Hey Guys,

    I hope i'm missing something here, but I am attempting to use
    UnityWebRequestMultimedia.GetAudioClip
    in order to download an audio stream from the web, and then, using
    DownloadHandlerAudioClip.GetContent
    to extract the clip and play it. This works perfectly fine in the Editor, Desktop and Mobile - however on web, it is unreliable. There are times when i can get it to work perfectly fine, but at other times i get an error in
    _JS_Sound_GetLoadState TypeError: Cannot read property 'error' of undefined
    .

    From what i can tell, this seems to be a race condition - either load has not been called for the audio buffer, or something caused the audio buffer state to be checked prior to the load being called. It is possible it could be a race condition with WebAudioInit, but this can happen after a piece of audio plays, so i assume at that point the audio library is initialised for the whole engine. This happens more frequently when the engine is performing a lot of work.

    Code (CSharp):
    1.         var www = UnityWebRequestMultimedia.GetAudioClip(audioPath, AudioType.MPEG);
    2.         yield return www.SendWebRequest();
    3.  
    4.         if (www.isNetworkError)
    5.         {
    6.             www.Dispose();
    7.             yield break;
    8.         }
    9.  
    10.         var Clip = DownloadHandlerAudioClip.GetContent(www); //crashes here
    11.         outputSource.clip = Clip;
    12.         outputSource.Play();
    I did also go into the audio js library and change the if condition to
    if (!sound || sound.error)
    which did get past that line of code - however the engine then crashes with
    Uncaught abort(84) at Error
    and
    Uncaught abort(171) at Error
     
  2. rdpeake

    rdpeake

    Joined:
    Jan 26, 2017
    Posts:
    5
    New Information - has no association with the application being busy - it's a memory use error.
     
  3. kognito1

    kognito1

    Joined:
    Apr 7, 2015
    Posts:
    331
  4. rdpeake

    rdpeake

    Joined:
    Jan 26, 2017
    Posts:
    5
    Hey, no it wasn't to do with supported formats - as smaller files would play fine. The issue seems to be with how unity plays compressed audio (though the errors when it goes wrong are not very intuitive). i ended up using the WebGLStreamingAudio assets from assets store.
     
  5. JugglerDancing

    JugglerDancing

    Joined:
    Oct 14, 2021
    Posts:
    3
    Sorry, but would you please show me the URL of "WebGLStreamingAudio assets"? I have faced the problem for a long time, Thank you very much!
     
    G3nko0 likes this.
  6. G3nko0

    G3nko0

    Joined:
    Oct 1, 2016
    Posts:
    12
    I have similar problem. There are no packages named "WebGLStreamingAudio" in assets store, could you share please what exactly you used to solve the problem?
     
  7. G3nko0

    G3nko0

    Joined:
    Oct 1, 2016
    Posts:
    12
    I have managed to resolve the issue with the following code:

    Code (CSharp):
    1. using UnityWebRequest www = UnityWebRequestMultimedia.GetAudioClip(audioUrl, AudioType.MPEG);
    2.         DownloadHandlerAudioClip dHA = new DownloadHandlerAudioClip(string.Empty, AudioType.MPEG);
    3.         dHA.streamAudio = true;
    4.         www.downloadHandler = dHA;
    5.         www.SendWebRequest();
    6.         while (www.downloadProgress < 1) {
    7.             Debug.Log(www.downloadProgress);
    8.             yield return new WaitForSeconds(.1f);
    9.         }
    10.         if (www.responseCode != 200 || www.result == UnityWebRequest.Result.ConnectionError) {
    11.             Debug.Log("error");
    12.         } else {
    13.             audioSource.clip = DownloadHandlerAudioClip.GetContent(www);
    14.             Debug.Log("Start audio play");
    15.             audioSource.Play();
    16.         }
     
    krishnabansal likes this.
  8. JugglerDancing

    JugglerDancing

    Joined:
    Oct 14, 2021
    Posts:
    3
    Thank you very much! it works fine in my environment too.
     
    Last edited: Jun 13, 2022
  9. RageAgainstThePixel

    RageAgainstThePixel

    Joined:
    Mar 11, 2020
    Posts:
    66
    I tried using this method, but getting these errors when trying to stream from remote resource URL:

    Code (CSharp):
    1. Playback of audio clip not yet possible; headers are done, 207516/? (66.09%) bytes downloaded but size is still not known
    2. Error: Cannot create FMOD::Sound instance for clip "" (FMOD error: The HTTP request timed out. )
    3.  
     
    Foreman_Dev likes this.
  10. Foreman_Dev

    Foreman_Dev

    Joined:
    Feb 4, 2018
    Posts:
    82
    I am getting the same exact error as @StephenHodgson-XRTK when trying to play a streaming audio clip in a WebGL build (Unity 2021.3.21f1)

    Would love it if a Unity engineer could take a look at this.
     
  11. abilsudarman

    abilsudarman

    Joined:
    Dec 27, 2023
    Posts:
    4
    I am getting the same exact error as @StephenHodgson-XRTK and @SpaceAgeInteractive . Anybody got it resolved? Unity 2021.3.33
    )
     
    Foreman_Dev likes this.
  12. RageAgainstThePixel

    RageAgainstThePixel

    Joined:
    Mar 11, 2020
    Posts:
    66
    btw I changed my handle from @StephenHodgson-XRTK to @RageAgainstThePixel. We are one and the same person.


    I ended up creating a custom http request type in my base rest library and creating small audio clips from chunked audio data as it came in.

    See reference here:
    https://github.com/RageAgainstThePi...tilities.rest/Runtime/Rest.cs#L232C36-L232C36
    Implementation here:
    https://github.com/RageAgainstThePi...ime/TextToSpeech/TextToSpeechEndpoint.cs#L190
     
    dhtpdud528 and Foreman_Dev like this.