Play mp3 file from url

Hi, I’m having some issue to play mp3 file from url with www.

I have few url in a string[ ] table name “urlsound”.
This is working pretty weirdly. I’m having some empty error message, so i don’t know where it come from. Sometimes this is working perfectly for one or 2 tracks and not working at the next track. And the sound can start after few seconds…don’t know why too.

Here is my code :

using UnityEngine;
using System.Collections;

public class managesound : MonoBehaviour {

    public string[] urlsound;
    private int i;
    public AudioSource audio;
    WWW www;
    public bool loading = false;

    public void startsound () { //start audio player
        i=Random.Range(0,urlsound.Length); //first track is random
        setup();
    }
    void Update () {
        if(loading && audio.clip.isReadyToPlay){ //when ready to play
            loading=false;
             audio.Play();
         }
    }
    public void nextsound () { //click next track
        i++;
        if(i>=urlsound.Length) i=0;
        setup();
    }
    public void previoussound () { //click previous track
        i--;
        if(i<0) i=urlsound.Length-1;
        setup();
    }
    void setup(){
        StartCoroutine(DownloadAndPlay());
    }
    IEnumerator DownloadAndPlay()
    {
        audio.Stop(); //reset www and AudioSource
        www=null;  //reset www and AudioSource
        audio.clip =null;//reset www and AudioSource
        www = new WWW (urlsound[i]); //load new track url
        audio.clip = www.GetAudioClip(false,true);
        loading=true;
    }
}

I’m not sure. Have you tried using www.audioClip instead of of GetAudioClip()? Does it produce the same weirdness?

I just tried and it’s working perfectly with www.audioClip
But most of my sounds are 10Mo+ and with www.audioClip i have to wait the end of the download to Play the music and it can be more than 30 seconds…

I’m not sure what the solution is. The GetAudioClip() method doesn’t give much control to how the audio is streamed. Is it possible to specify how much data is needed before streaming? I’ve no idea.

I guess there’s a good reason why you are not simply bundling the audio into the game. Perhaps you can try further compressing the audio files to make them smaller? Or maybe there exists a better third party file streaming plugin?