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 How play audio (mp3) from server (URL)?

Discussion in 'Audio & Video' started by EddieRATIGNIER, May 22, 2017.

  1. EddieRATIGNIER

    EddieRATIGNIER

    Joined:
    May 21, 2017
    Posts:
    8
    Hi!

    I have a big problem with my project Unity!
    Indeed, I need to read many musics (for a blindtest) and thus I would like to pass by a server to upload my musics (to avoid having an application weighing several Go).

    I use the class WWW which works, but every music puts at least 30 seconds/1 minute before playing... How to have the music immediatly?

    Furthermore, I would like to use as URL a "compartment" of a array wich contain all the links. But impossible to do it because I have an error message.

    Here is the code which I use :

    Code (CSharp):
    1. IEnumerator loadMusic()
    2.     {
    3.         string[] Col = BlindTest[2].Split(',');
    4.         string url = Col[5];
    5.         GameObject Musique = GameObject.Find("Musique");
    6.         AudioSource audioSource = Musique.GetComponent<AudioSource>();
    7.         WWW music = new WWW(url);
    8.         yield return music;
    9.         AudioClip lamusic = music.GetAudioClipCompressed(true, AudioType.MPEG);
    10.         audioSource.clip = lamusic;
    11.         audioSource.Play();
    12.     }
    Thanks for your help!
     
    khanhabib likes this.
  2. EddieRATIGNIER

    EddieRATIGNIER

    Joined:
    May 21, 2017
    Posts:
    8
  3. r618

    r618

    Joined:
    Jan 19, 2009
    Posts:
    1,302
    you are downloading the whole audio file prior to playing - that's why it always take long before playback begins
    use streaming flag for getting the audio clip:
    Code (CSharp):
    1. AudioClip lamusic = music.GetAudioClip(true, true, AudioType.MPEG);
     
  4. r618

    r618

    Joined:
    Jan 19, 2009
    Posts:
    1,302
    I'm not sure what you mean by compartment, but you can define a list of urls to be accessible in inspector, fill the values there, and then iterate on the list as needed:
    Code (CSharp):
    1. public class Loader: MonoBehaviour
    2. {
    3.     public List<string> urls;
    4.     ...
    5.     public void PlayMusic(int i)
    6.     {
    7.        StarCoroutine(this.PlayMusicWithUrl(this.urls[i]));
    8.     }
    9.     ...
    10. }
     
  5. EddieRATIGNIER

    EddieRATIGNIER

    Joined:
    May 21, 2017
    Posts:
    8
    For the both methods, there is the same problem... There is no probleme to play a file from my PC; but to play a song upload on a server, there is a lot of latency before the song is played...
     
  6. r618

    r618

    Joined:
    Jan 19, 2009
    Posts:
    1,302
    @EddieRATIGNIER I've just tried a 65 MB oggvorbis file hosted on VPS, and with
    Code (CSharp):
    1. music.GetAudioClipCompressed
    I got roughly 1m 40s until playback started, with
    Code (CSharp):
    1. music.GetAudioClip
    cca 1m 4s
    / Unity 5.5.3f1 winx64 /

    I definitely expected the latter to be much (much) quicker, so there's definitely something not right.

    At the moment I can only recommend my plugin (link in the signature... ) where the playback started under 2 seconds.

    Discovered one caveat with AudioStream though: the dropbox links do not seem to work - presumably due to how dropbox handles secure links redirection, which was changed over time :-[
     
    Last edited: May 28, 2017
  7. EddieRATIGNIER

    EddieRATIGNIER

    Joined:
    May 21, 2017
    Posts:
    8
    41s with a 3 MB .mp3 file... I does not really understand ^^

    I would like to use your plugin but $40 is too much for me, especially for the application which I want create (just for me and some friends).

    You don't have another way simply to read a .mp3 file upload on a VPS without waiting time?
     
  8. r618

    r618

    Joined:
    Jan 19, 2009
    Posts:
    1,302
    It certainly does not look like it, things to try :
    - try actual standalone / mobile build of the application, editor results might not be accurate ( although I doubt the result will be better / )
    - try different Unity versions - presumably even around 4.7
    - new UnityWebRequest does not have yet audio streaming handler implemented last time I checked
    - this is worth filing a bugrepot
    - if you don't want to spend money you can download and use the FMOD Unity plugin yourself ( my package just uses it in a nice way for user, and provides integration with AudioSource )
    - there are few other audio solutions e.g. from wwise, or even NAudio for .net which are worth checking out and might help

    that's probably all i can think of right now
     
    Last edited: May 28, 2017
  9. EddieRATIGNIER

    EddieRATIGNIER

    Joined:
    May 21, 2017
    Posts:
    8
    You know if in previous version of Unity, the streaming (WWW.GetAudioClip) work without latency?
    And you can help me to find a solution? Because i'm beginner and it's very difficult for me to find another way... especially when there is no tutorial on the Internet... :/
     
    khanhabib likes this.
  10. r618

    r618

    Joined:
    Jan 19, 2009
    Posts:
    1,302
    @EddieRATIGNIER I've used stream links of internet radios intensively around 4.1-4.4 I think and those worked, I'm not 100% sure about hosted files;
    But it's easy to find out: the code would be the same as what you have now, just use GetAudioClip with streaming flag

    If you encounter any problem just post here

    I might be able to try it out later with 4.7 and 5.3
     
  11. r618

    r618

    Joined:
    Jan 19, 2009
    Posts:
    1,302
    Looks like streaming of hosted files using GetAudioClip is/was broken all along:
    4.7
    dropbox ~ 13 sec
    VPS more than 3 min

    5.3
    dropbox ~ 13 sec
    VPS ~ 3:00 min

    5.5
    dropbox ~ 13 sec
    VPS 2 - 3 min

    From the above it looks like the whole file is downloaded in full each time even with stream flag set ( dropbox gets full bandwidth, my crappy VPS does some things to limit it )

    On the behalf of the internet I would like to apologize for the incorrect advice stated in my first reply @EddieRATIGNIER :)

    At this point there are not good AssetStore free options, esp. since you are new to unity - plugging separate network audio stream into AudioSource is little bit more involved;

    I'd maybe consider not use unity at all, if possible, e.g. https://github.com/naudio/NAudio/tree/master/NAudioDemo/Mp3StreamingDemo

    Bit unfortunate, but it looks like it is/was not highly demanded feature thus being left basically overlooked (FMOD itself which Unity uses allows it)
     
    Last edited: May 30, 2017
    EddieRATIGNIER likes this.
  12. EddieRATIGNIER

    EddieRATIGNIER

    Joined:
    May 21, 2017
    Posts:
    8
    I am going to try NAUDIO.
    If I have big problems, I come back to you!

    Thank you for the help! :)
     
    r618 likes this.
  13. EddieRATIGNIER

    EddieRATIGNIER

    Joined:
    May 21, 2017
    Posts:
    8
    Ok... I tried to analyze the code of the demo project but I dont understand all the code. Indeed, it's really complex for me ... You can look it?

    The application demo is very amazing because it work perfecly without delay!
     
  14. r618

    r618

    Joined:
    Jan 19, 2009
    Posts:
    1,302
  15. EddieRATIGNIER

    EddieRATIGNIER

    Joined:
    May 21, 2017
    Posts:
    8
    Oh no :( I spent a lot of time on the UI, the code and the listing of hundreds musics for nothing finally :/

    In any case, thank you very much to you for the help which you brought to me!
     
  16. r618

    r618

    Joined:
    Jan 19, 2009
    Posts:
    1,302
    Sorry to hear that, but thanks for understanding. 0)
     
    EddieRATIGNIER likes this.
  17. Mayank516

    Mayank516

    Joined:
    Nov 17, 2014
    Posts:
    7