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

Audio Streaming audio with UnityWebRequest from server

Discussion in 'Audio & Video' started by GospelOfMyHeart, May 19, 2019.

  1. GospelOfMyHeart

    GospelOfMyHeart

    Joined:
    May 19, 2019
    Posts:
    2
    Hello, i have a problem over here and can`t figure out, how to make a streaming audio download from server. I mean, there is an audio file on server, and i need not to download and play it, but play it while it`s downloading and save for the runtime(not to cache it or save somewhere on disk). For both PC and mobile platform. For now, i can only download and play it. Maybe there is a solution with creating audio from downloaded bytes? Code below:
    Code (CSharp):
    1.  
    2. public class StreamingAudio:MonoBehaviour
    3. {
    4.    public AudioSource audioSource;
    5.    public string URL;
    6.    public void Start()
    7.     {
    8.         StartCoroutine(GetData());
    9.     }
    10.  
    11.    public IEnumerator GetData()
    12.     {
    13.         // Creating download handler
    14.         DownloadHandlerAudioClip downloadHandler = new DownloadHandlerAudioClip("", AudioType.OGGVORBIS);
    15.         //Make audio streaming
    16.         downloadHandler.streamAudio = true;
    17.        
    18.         UnityWebRequest request = new UnityWebRequest(URL, "GET", downloadHandler, null);
    19.  
    20.         // downloading audio file
    21.         request.SendWebRequest();
    22.         AudioClip audioClip = null;
    23.         //checking is there is clip
    24.         while (audioClip == null )
    25.         {
    26.          
    27.             try
    28.             {
    29.                 //trying to recieve audio
    30.                 audioClip = DownloadHandlerAudioClip.GetContent(request);    
    31.             }
    32.             catch (Exception e) {
    33.                 //returns message that audio download not finished
    34.                 Debug.Log("Exception: " + e.Message);
    35.             }
    36.              //checking downloadedBytes size (maybe i can do smthg with these bytes?)
    37.              Debug.Log("Загружено байтов: "+ request.downloadedBytes );
    38.             yield return 0f;
    39.         }
    40.  
    41.         // playing audio source on download finished
    42.         audioSource.clip = audioClip;
    43.         audioSource.Play();
    44.  
    45.         yield return null;
    46.     }
    47. }
     
  2. Jean-Fabre

    Jean-Fabre

    Joined:
    Sep 6, 2007
    Posts:
    429
    Hi,

    Get the same problem, Did you get a resolution on this?

    Bye,

    Jean
     
  3. BluudNobbut

    BluudNobbut

    Joined:
    Aug 31, 2019
    Posts:
    3
    Have you found any solution to this and I would love to know the possible solution to it? Soon after you get it can you post the solution here.
     
  4. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,539
  5. RageAgainstThePixel

    RageAgainstThePixel

    Joined:
    Mar 11, 2020
    Posts:
    66
    I have not been able to get this to work either. I think the streaming flag is ignored. I tried Invertex's example too but no dice.
     
  6. RageAgainstThePixel

    RageAgainstThePixel

    Joined:
    Mar 11, 2020
    Posts:
    66
    I noticed that no matter what the downloadHandler.audioClip.loadType is DecompressOnLoad instead of Streaming.
     
  7. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,539
    Strange, are you loading the audio clip from local files or a web server? My example worked fine for me playing an MP3 from a web server.

    What Unity version as well? I tested it on 2022.2.1
     
  8. RageAgainstThePixel

    RageAgainstThePixel

    Joined:
    Mar 11, 2020
    Posts:
    66
    I'm streaming from a web server using 2021.3.x

    For reference it's this project:
    https://github.com/RageAgainstThePixel/com.rest.elevenlabs
     
  9. abilsudarman

    abilsudarman

    Joined:
    Dec 27, 2023
    Posts:
    4
    I really need this too. From a text to speech on my webserver. Does anyone figured it out?
     
  10. abilsudarman

    abilsudarman

    Joined:
    Dec 27, 2023
    Posts:
    4
  11. abilsudarman

    abilsudarman

    Joined:
    Dec 27, 2023
    Posts:
    4
    after messing with the bytes and correcting audio type, i got this error instead. Anyone having the same thing?
     

    Attached Files: