Search Unity

Audio How to play an audio stream?

Discussion in 'Audio & Video' started by Lampogolovii_, Apr 10, 2018.

  1. Lampogolovii_

    Lampogolovii_

    Joined:
    Aug 28, 2017
    Posts:
    10
    Hi,
    I need a radio player for my game.
    I have an URL and I want to play the audio from it.

    I've tried this code:
    Code (CSharp):
    1. public void Play()
    2.     {
    3.         StartCoroutine( PlayAudioStrem() );
    4.     }
    5.     IEnumerator PlayAudioStrem()
    6.     {
    7.         WWW music = new WWW(URL);
    8.         AudioClip clip = music.GetAudioClip(false, true, AudioType.MPEG);
    9.         while (!clip || !clip.isReadyToPlay)
    10.             yield return new WaitForSeconds(0.1f);
    11.      
    12.         if (clip)
    13.         {
    14.             GetComponent<AudioSource>().clip = clip;
    15.             GetComponent<AudioSource>().Play();
    16.         }
    17.     }
    The music is playing for several seconds and then stops.

    Using Unity2017.
    Heard, that WWW is the old class.

    I tried UnityWebRequestMultimedia.GetAudioClip(). But I think it works only with files. I can't play the audio stream, only download files.

    Please help. What should I use to listen audio stream?
     
  2. r618

    r618

    Joined:
    Jan 19, 2009
    Posts:
    789
    this popped up before, I ended up gifting my asset to the dude ;0)
    We were able to correct some edge cases with streaming though, at least.
    I'll have a look on how UnityWebRequest in 2017 stands, but give me some time plx
     
  3. r618

    r618

    Joined:
    Jan 19, 2009
    Posts:
    789
    Also please post the URL you want to stream
     
  4. Lampogolovii_

    Lampogolovii_

    Joined:
    Aug 28, 2017
    Posts:
    10
    this one: http://a6.radioheart.ru:8012/live (https works fine too)

    about UnityWebRequest.
    I tried this way:

    Code (CSharp):
    1.         UnityWebRequest music = UnityWebRequestMultimedia.GetAudioClip(URL, AudioType.MPEG);
    2.         yield return music.SendWebRequest();
    3.  
    4.         if (music.isNetworkError)
    5.         {
    6.             Debug.Log(music.error);
    7.         }
    8.         else
    9.         {
    10.             AudioClip clip = DownloadHandlerAudioClip.GetContent(music);
    11.             Debug.Log( clip + " length: " + clip.length );
    12.             if (clip)
    13.             {
    14.                 GetComponent<AudioSource>().clip = clip;
    15.                 GetComponent<AudioSource>().Play();
    16.             }
    17.         }
    And it looped in
    yield return music.SendWebRequest();
    I think it tries to download entire mp3 or something.
     
    Last edited: Apr 11, 2018
  5. r618

    r618

    Joined:
    Jan 19, 2009
    Posts:
    789
    yep

    overall:
    - I think previous WWW.GetAudioClip is completely broken from 5.5. up to 2017.4 I've tested it with
    first of all it is necessary to either wait hardcoded time or wait until WWW.progress is reported to be around 0.95 before assigning GetAudioClip clip to an AudioSource
    ( while progress is updated in 2017.4, but not in 5.4.5. )
    - despite having stream flag set I was never able to stream more than few seconds as you mentioned before
    - 'yield return www;' does not finish in all cases, so can be forgotten
    - asking for audioSource.clip.loadState produced FMOD errors in Console on 5.5.4 - this seemed to be fixed to a degree in 2017.4 at least, though audio still wouldn't play more than few seconds
    (I used WWW.GetAudioClip with streaming more or less without problems on 4.x versions, I think starting with 5 cycle is where the changes started to be made)

    So the best course of action if you don't want to shelve some monies for a streaming plugin is to constantly bug Unity to implement actual streaming audio handler for UnityWebRequestMultimedia going forward since I think they have no intentions fixing older WWW.GetAudioClip given the state I found it now ;;
     
  6. Lampogolovii_

    Lampogolovii_

    Joined:
    Aug 28, 2017
    Posts:
    10
    I see. And what about your asset? Does it work? Could you please test it with my audio stream. I'm thinking about purchasing a plugin for this issue.
     
  7. techblogger911

    techblogger911

    Joined:
    Apr 7, 2018
    Posts:
    3
    I don't want to waste money on this for the older version should work fine as well
     
  8. r618

    r618

    Joined:
    Jan 19, 2009
    Posts:
    789
    you can download demo from asset store page ( I think I mentioned it before ) and enter url into it
    but yes, your url seems to be working with it
     
  9. r618

    r618

    Joined:
    Jan 19, 2009
    Posts:
    789
    oh you can try using other libraries, too -
    fmod unity intergrations directly (which my plugins uses), NAudio should do it too, and possibly others like bass.net or native libraries for specific platforms
     
    Last edited: Apr 12, 2018
  10. Lampogolovii_

    Lampogolovii_

    Joined:
    Aug 28, 2017
    Posts:
    10
    Thanks for your answer!
    Trying fmod now. Imported fmod.unitypackage to my project.
    Do nothing and get BusNotFoundException: FMOD Studio bus not found 'bus:/' on Unity play button.
    There is something about fmod studio integration in "getting started". But I need just to play audio stream. Should I install fmod studio and connect fmod project to the Unity somehow?
     
  11. Lampogolovii_

    Lampogolovii_

    Joined:
    Aug 28, 2017
    Posts:
    10
    A-a-a-a-a!
    It works! (created empty project - and that's it) Great thanks to you, r618!
     
  12. r618

    r618

    Joined:
    Jan 19, 2009
    Posts:
    789
    It might be possible to stream from a network location directly in FMOD Studio project, but I've never tried it in fact, I'm using only low level c# wrapper from the integrations (i.e. without fmod studio project)
     
  13. r618

    r618

    Joined:
    Jan 19, 2009
    Posts:
    789
    btw they seem to be having a crack at this in 2018.2, from latest beta 2018.2.0b1 release notes:

    Scripting: TLS 1.2 support via .Net45 APIs (SslStream, HttpRequest, ...) across all platforms.
    Web: Added streamAudio property to DownloadHandlerAudioClip, which lets getting a playable audio clip before download is complete
    Audio: Fixed non-readable errors in some cases with streaming audio clips from WWW (764650)
    Audio: Fixed streaming an audio clip from WWW directly, without waiting for the download being ready - would throw errors in some cases previously (574945)

    Looks like I submitted one of the referenced issues back in 2013, for 4.3.0 (°,,°)
     
  14. Lampogolovii_

    Lampogolovii_

    Joined:
    Aug 28, 2017
    Posts:
    10
    For me it doesn't work without an fmod studio project. Will try to figure it out later. Now it works and I'm fine :)

    Wow! It would be great! I'm on 2018.1 now. I'll try when it releases from beta.
     
  15. GospelOfMyHeart

    GospelOfMyHeart

    Joined:
    May 19, 2019
    Posts:
    2
    Please, can someone help to deal with streaming audio. Using Unity to stream audio, i ended up receiving "
    Error: Cannot create FMOD::Sound instance for resource 0x*: , (Unsupported file or audio format. )" while trying to receive unfinished audio from download handler. streamAudio property turned to true. Heeeeeeeelp
     
  16. thelghome

    thelghome

    Joined:
    Jul 23, 2016
    Posts:
    37
    FM Exhibition Tool Pack | Forum
    It might be a bit off-topic, but we did some research about audio streaming recently and released a feature related to audio stream.
    It's an interesting solution for capturing in-game audio(server) and playback on other devices(clients) remotely.
    Mac/PC/iOS/Android
     
  17. ullas-vk

    ullas-vk

    Joined:
    Jun 27, 2017
    Posts:
    1
    It is not working in iOS build