I am trying to stream a clip from the Internet when a GUI.Button is pressed. Every time I run the program the clip does not have enough time to download without a yield download. But if I put it into the OnGUI function it gives an error whereas it cannot be a coroutine.
Here is a sample of my code:
var path = "url name";
var buttonSize = Vector2(100,30);
var download = new WWW (path);
function Start()
{
}
function OnGUI()
{
var clip : AudioClip = download.oggVorbis;
if(GUI.Button(Rect(Screen.width/2 - buttonSize.x, Screen.height/2 - buttonSize.y, buttonSize.x, buttonSize.y), "Play Audio"))
audio.clip = clip;
audio.Play();
}
function update()
{
}
Please let me know a legitimate solution to this problem.
Thanx
I think you will have to separate out the download into a coroutine, so a possible solution is to use the gui call to setup the download and then check if the clip is ready in the update function… something like
var path = "url name";
var buttonSize = Vector2(100,30);
var download = new WWW (path);
var clip : AudioClip;
function Start()
{
}
function OnGUI()
{
if(GUI.Button(Rect(Screen.width/2 - buttonSize.x, Screen.height/2 - buttonSize.y, buttonSize.x, buttonSize.y), "Play Audio"))
{
if(null != audio.clip)
{
// download the clip
// (in the download coroutine set the clip to the download.oggVorgis)
}
}
}
function Update()
{
if(null != audio.clip)
{
// play the clip
}
}
If you were really worried about the audioclip being ready, you could download it on Start so that it was already there, and just call audio play from the gui button.
Use a helper function for your coroutine, as soon as the video clip is ready to play you can initiate playback on it (just leave yield out of OnGUI, Start, Update, etc.).