Search Unity

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:
    336
    Hi,

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

    Bye,

    Jean
     
  3. BluudNobbut

    BluudNobbut

    Joined:
    Aug 31, 2019
    Posts:
    1
    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.