Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Unity 5.5.0f3 Audioclip.lenght is 0 in WebGL

Discussion in 'Web' started by tcosta29, Jan 26, 2017.

  1. tcosta29

    tcosta29

    Joined:
    Apr 27, 2016
    Posts:
    6
    Hi guys!
    I'm having some kind of problem when I'm trying to retrieve the length of an audioclip.
    I'm loading the audio from the StreamingAssets folder, and the audio does play, but for some reason the audioClip.lenght is returning 0. Am I doing something wrong, or is this some kind of bug?
    I need to know the length of the audio file so I can sync it with some subtitles and other stuff.
    Any kind of help would be appreciated.
    Thanks!

    Code (CSharp):
    1.    using (UnityWebRequest www = UnityWebRequest.GetAudioClip(audioPath, AudioType.MPEG))
    2.             {
    3.                 yield return www.Send();
    4.  
    5.                 if (www.isError)
    6.                 {
    7.                     DebugController.OutputLogError("[AudioController] Error " + www.error + " Url: " + www.url);                        
    8.                 }
    9.                 else
    10.                 {
    11.                     DebugController.OutputLog("[AudioController] Success! WWW - " + www.url, true);
    12.  
    13.                     AudioClip audioClip = DownloadHandlerAudioClip.GetContent(www);
    14.  
    15.                     Debug.Log("[AudioController] Sound Played audioClip.length " + audioClip.length + " Name " + audioClip.name);
    16.                     audioSource.clip = audioClip;
    17.                     audioSource.Play();                              
    18.                 }
    19.              }
     
  2. Drakulo

    Drakulo

    Joined:
    Oct 31, 2012
    Posts:
    6
    Hello there.

    I faced the same issue in Unity 2019.2.21f1. Once you get the result of the web request, you also have to wait for the sound to be completely loaded in order to access the length of it. Here is an example :

    Code (CSharp):
    1. using (UnityWebRequest www = UnityWebRequestMultimedia.GetAudioClip(soundURL, AudioType.AUDIOQUEUE))
    2. {
    3.     yield return www.SendWebRequest();
    4.  
    5.     if (www.isNetworkError)
    6.     {
    7.         Debug.Log(www.error);
    8.     }
    9.     else
    10.     {
    11.         AudioClip clip = DownloadHandlerAudioClip.GetContent(www); ;
    12.         m_AudioSource.clip = clip;
    13.         while(clip.loadState != AudioDataLoadState.Loaded)
    14.         {
    15.             yield return null;
    16.         }
    17.  
    18.         // Now you can access the length of the audio file :
    19.         Debug.Log(m_AudioSource.clip.length);
    20.        
    21.     }
    22. }
    Cheers !