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

WebGL: play audio from URL

Discussion in 'WebGL' started by G3nko0, Jun 6, 2022.

  1. G3nko0

    G3nko0

    Joined:
    Oct 1, 2016
    Posts:
    12
    Description:
    I have an issue with playing audio from url on webgl platform.
    First time audio is played OK, the next call to play audio crashes app with the following error dialog (complete log attached):
    Screenshot 2022-06-06 at 20.40.56.png

    I tried to locate the problem and understand the error more and seems like it crashes on
    this line
    Code (CSharp):
    1. AudioClip clip = DownloadHandlerAudioClip.GetContent(uwr)
    I tried to find in the documentation any guideline on how to deal with audio on WebGL and I see some limitations mentioned but for me it still not clear is it possible to do or not?

    Environment:
    Unity 2021.3.1f1
    Chrome Version 102.0.5005.61 (Official Build) (arm64)

    Log:
    Complete log of error

    Code:
    Code (CSharp):
    1.         using (UnityWebRequest uwr = UnityWebRequestMultimedia.GetAudioClip(audioUrl, AudioType.MPEG)) {
    2.        
    3.             var response = uwr.SendWebRequest();
    4.             yield return response;
    5.             if (uwr.isNetworkError) {
    6.                 Debug.Log(uwr.error);
    7.             } else {
    8.                 AudioClip clip = DownloadHandlerAudioClip.GetContent(uwr);
    9.                 // Play audio clip logic
    10.             }
    11.         }
     
    Last edited: Jun 7, 2022
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    4,063
    Do you cache the clip or download it every time it is played?
     
  3. G3nko0

    G3nko0

    Joined:
    Oct 1, 2016
    Posts:
    12
    well its cached by server, its cached by browser, I don't see reason to cache one more time - yes I download it everytime for now
     
  4. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    4,063
    Just a few things I would try:

    Can you play the downloaded clip twice in a sequence? (ie download it once, assign it to clip, then play clip twice - with or without waiting for the first playback to finish)

    If that fails, can you play the same clip multiple times when the audio is embedded in the build?

    Test it on a different machine and different browsers to check if the behaviour is consistent.
     
  5. G3nko0

    G3nko0

    Joined:
    Oct 1, 2016
    Posts:
    12
    No, I think my problem is similar to this one. Currently tried to use this solution and it works but audio is distorted
     
  6. G3nko0

    G3nko0

    Joined:
    Oct 1, 2016
    Posts:
    12
    I have figured out myself, this code works for me:

    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.         }