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:
    12
    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:
    1,305
    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:
    1,305
    Also please post the URL you want to stream
     
  4. Lampogolovii_

    Lampogolovii_

    Joined:
    Aug 28, 2017
    Posts:
    12
    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:
    1,305
    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:
    12
    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:
    1,305
    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:
    1,305
    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:
    12
    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:
    12
    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:
    1,305
    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:
    1,305
    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:
    12
    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
     
    sgc109109 likes this.
  16. thelghome

    thelghome

    Joined:
    Jul 23, 2016
    Posts:
    741
    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
     
  18. jsull1

    jsull1

    Joined:
    Apr 3, 2018
    Posts:
    121
    Hey guys, I'm aware that this post is long dead but I make music visualizers and a dj wants me to use it for his live shows. Where do I start to make my project stream the audio output of his computer?
     
  19. SuperDanOsbourne

    SuperDanOsbourne

    Joined:
    Oct 3, 2017
    Posts:
    46

    this guy does some tutorials on visualizers and has some on using your mic as AudioSource
     
  20. unity_BTx7JZ418quE1Q

    unity_BTx7JZ418quE1Q

    Joined:
    Feb 26, 2020
    Posts:
    2
    Hi,
    Old thread but, where did you enter your live url for Fmod, I'm looking in Fmod Unity asset, nothing, in Fmod studio, nothing...
    Any Idea?
    Thanks
     
  21. Mayank516

    Mayank516

    Joined:
    Nov 17, 2014
    Posts:
    7
    Hi ,
    It might be late but can help other devs searching for similar functionality for iOS and Android. I have used following plugins to stream audios from web servers without downloading. The audio visualizer asset for android is also very helpful. Developer is helpful too. Links :
    iOS : https://assetstore.unity.com/packages/tools/audio/audio-stream-in-ios-182585
    Android : https://assetstore.unity.com/packages/tools/integration/audio-stream-in-android-196464
    Android Visualizer : https://assetstore.unity.com/packages/tools/integration/audio-visualizer-for-android-38377
     
  22. RageAgainstThePixel

    RageAgainstThePixel

    Joined:
    Mar 11, 2020
    Posts:
    66
    Years later and streaming still doesn't work haha.

    it seems that the AudioClip created by the UnityWebRequest still doesn't respect the streaming load type.
     
    polytropoi, Foreman_Dev and Radomiej like this.
  23. mcguinnessb6

    mcguinnessb6

    Joined:
    Dec 21, 2022
    Posts:
    2

    Ha yeah, here's my solution using NAudio
    https://github.com/AstralSkies/RadioUnityStream
     
    RageAgainstThePixel likes this.
  24. RageAgainstThePixel

    RageAgainstThePixel

    Joined:
    Mar 11, 2020
    Posts:
    66
    Great! but I don't wanna embed another plugin :(

    Plus the license doesn't work for me as I need it for commercial applications