How play audio (mp3) from server (URL)?

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 :

IEnumerator loadMusic()
    {
        string[] Col = BlindTest[2].Split(',');
        string url = Col[5];
        GameObject Musique = GameObject.Find("Musique");
        AudioSource audioSource = Musique.GetComponent<AudioSource>();
        WWW music = new WWW(url);
        yield return music;
        AudioClip lamusic = music.GetAudioClipCompressed(true, AudioType.MPEG);
        audioSource.clip = lamusic;
        audioSource.Play();
    }

Thanks for your help!

1 Like

Help ^^

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:

AudioClip lamusic = music.GetAudioClip(true, true, AudioType.MPEG);

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:

public class Loader: MonoBehaviour
{
    public List<string> urls;
    ...
    public void PlayMusic(int i)
    {
       StarCoroutine(this.PlayMusicWithUrl(this.urls[i]));
    }
    ...
}

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…

@EddieRATIGNIER I’ve just tried a 65 MB oggvorbis file hosted on VPS, and with music.GetAudioClipCompressed I got roughly 1m 40s until playback started, with 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 :-[

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?

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

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… :confused:

1 Like

@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

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 :slight_smile:

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. NAudio/NAudioDemo/Mp3StreamingDemo at master · naudio/NAudio · GitHub

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)

1 Like

I am going to try NAUDIO.
If I have big problems, I come back to you!

Thank you for the help! :slight_smile:

1 Like

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!

@EddieRATIGNIER Sorry, I can’t implement the whole thing

Oh no :frowning: I spent a lot of time on the UI, the code and the listing of hundreds musics for nothing finally :confused:

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

Sorry to hear that, but thanks for understanding. 0)

1 Like

I have used below plugins for streaming audios on iOS and Android and they work out of the box. Developer is also very helpful. Provided support when ever was required.

Android : Audio Stream in Android | Integration | Unity Asset Store

iOS : https://assetstore.unity.com/packages/tools/audio/audio-stream-in-ios-182585