Search Unity

  1. Unity 2020.2 has been released.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Help Wanted How to loop the sound downloaded from the Internet (streaming mode)?

Discussion in 'Scripting' started by scorp2007, Jun 22, 2020.

  1. scorp2007

    scorp2007

    Joined:
    Aug 17, 2014
    Posts:
    44
    Hello.
    I want the sound I download and play to be looped, how can I do this? The problem is that if start to play it before it loads completely, then it cannot be looped, but I need to play it in streaming mode, because the files are quite large. I load it like this:

    Code (CSharp):
    1.     IEnumerator SongCoroutine(string path)
    2.     {
    3.         using (UnityWebRequest www = UnityWebRequestMultimedia.GetAudioClip(path, AudioType.OGGVORBIS))
    4.         {
    5.             DownloadHandlerAudioClip dHA = new DownloadHandlerAudioClip(string.Empty, AudioType.OGGVORBIS);
    6.             dHA.streamAudio = true;
    7.             dHA.compressed = true;
    8.             www.downloadHandler = dHA;
    9.             www.SendWebRequest();
    10.  
    11.             while (www.downloadProgress < 1.0f)
    12.             {
    13.                 if (www.downloadProgress > 0.01f && asource.clip == null)
    14.                 {
    15.                     asource.clip = dHA.audioClip;
    16.                     asource.Play();
    17.                 }
    18.  
    19.                 //Debug.Log(www.downloadProgress);
    20.                 yield return new WaitForSeconds(.1f);
    21.             }
    22.  
    23.             if (www.isNetworkError)
    24.             {
    25.                 Debug.Log("error");
    26.             }
    27.             else
    28.             {
    29.                 Debug.Log("FULL LOAD " + item.clipUrl);
    30.             }
    31.         }
    32.     }
    I tried different options to make it loop, but nothing worked. Please, help.
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    4,761
    If you've downloaded it and it's playing once, we can safely ignore the entire downloading part of this problem. It's just a question of how do I make a sound loop?

    Can you show what you tried in that vein? Generally you just have to enable loopin on the AudioSource and play the sound with Play() (NOT PlayOneShot)
     
  3. scorp2007

    scorp2007

    Joined:
    Aug 17, 2014
    Posts:
    44
    This does not work, otherwise I would not have created such a topic. I tried to create a new AudioSource at the end of the download and play the sound there, tried to set the time of AudioSource 0 and play it curtain, in general, nothing works from this.

    If I start playing sound only after it is fully loaded - then it works, but I need the sound to play before the file is fully loaded (streaming).

    This is due to the streaming mode and I do not know how to get around this.
     
  4. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    2,767
  5. scorp2007

    scorp2007

    Joined:
    Aug 17, 2014
    Posts:
    44
    yes AudioSource=true and its dont work. Preale see attached package (Unity 2019.3.13f1), it uses short sound for testing, but I will use it for big sounds, so streamAudio = true; required. If you tell me how to loop this example, I will be grateful. Thanks.
     

    Attached Files:

  6. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    2,767
    If it does loop with streamAudio = false, but doesn't when it's true, then it looks like a bug to me. If so, please report it (link this thread in report and paste bug number here).
     
  7. scorp2007

    scorp2007

    Joined:
    Aug 17, 2014
    Posts:
    44
    This works even with streamAudio = true, but only if I start to play sound after it is fully downloaded, if I start to play the file during download, it is impossible to loop it, even after it download fully.

    can't loop:
    Code (CSharp):
    1.     IEnumerator SongCoroutine(string path)
    2.     {
    3.         using (UnityWebRequest www = UnityWebRequestMultimedia.GetAudioClip(path, AudioType.OGGVORBIS))
    4.         {
    5.             DownloadHandlerAudioClip dHA = new DownloadHandlerAudioClip(string.Empty, AudioType.OGGVORBIS);
    6.             dHA.streamAudio = true;
    7.             dHA.compressed = true;
    8.             www.downloadHandler = dHA;
    9.             www.SendWebRequest();
    10.  
    11.             while (www.downloadProgress < 1.0f)
    12.             {
    13.                 if (www.downloadProgress > 0.01f && asource.clip == null)
    14.                 {
    15.                     asource.clip = dHA.audioClip;
    16.                     PlayOn();
    17.                 }
    18.  
    19.                 Debug.Log(www.downloadProgress);
    20.                 yield return new WaitForSeconds(.1f);
    21.             }
    22.  
    23.             if (www.isNetworkError)
    24.             {
    25.                 Debug.Log("error");
    26.             }
    27.             else
    28.             {
    29.                 //asource.clip = dHA.audioClip;
    30.                 //PlayOn();
    31.  
    32.                 Debug.Log("FULL LOAD " + item.clipUrl);
    33.             }
    34.         }
    35.     }
    can loop:
    Code (CSharp):
    1.     IEnumerator SongCoroutine(string path)
    2.     {
    3.         using (UnityWebRequest www = UnityWebRequestMultimedia.GetAudioClip(path, AudioType.OGGVORBIS))
    4.         {
    5.             DownloadHandlerAudioClip dHA = new DownloadHandlerAudioClip(string.Empty, AudioType.OGGVORBIS);
    6.             dHA.streamAudio = true;
    7.             dHA.compressed = true;
    8.             www.downloadHandler = dHA;
    9.             www.SendWebRequest();
    10.  
    11.             while (www.downloadProgress < 1.0f)
    12.             {
    13.                 if (www.downloadProgress > 0.01f && asource.clip == null)
    14.                 {
    15.                     //asource.clip = dHA.audioClip;
    16.                     //PlayOn();
    17.                 }
    18.  
    19.                 Debug.Log(www.downloadProgress);
    20.                 yield return new WaitForSeconds(.1f);
    21.             }
    22.  
    23.             if (www.isNetworkError)
    24.             {
    25.                 Debug.Log("error");
    26.             }
    27.             else
    28.             {
    29.                 asource.clip = dHA.audioClip;
    30.                 PlayOn();
    31.  
    32.                 Debug.Log("FULL LOAD " + item.clipUrl);
    33.             }
    34.         }
    35.     }
    But as I said above, I need so that I can play the sound until it is fully loaded, and at the same time so that it can be looped when the full download is complete.
     
  8. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    2,767
    In any case this looks like a bug to me, please report it, we'll have a deeper look.
     
unityunity