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;
}
}