- Home /
How to avoid lag when using WWW class to download sound
i have the following code to load a sound from a server that works:
string url = "some url";
WWW www = new WWW(url);
yield return www;
atmosphereClips[scene][mood][sound] = www.oggVorbis;
But the problem with this code is that it lags a lot when it comes to "AudioClip someAudioClip = www.oggVorbis;" and it is not that big an ogg file that is being downloaded. I think it is because Unity
I have also tried to use WWW.audioClip which does not lag but as I have to download more than one sound in succession (to fill the atmosphereClips array) I don't think I can use that. I have not been able to detect when it was finished at least.
Hope someone has a better, more lag free way to do this.
Answer by springer · Jul 26, 2010 at 10:41 PM
from the answer:
http://answers.unity3d.com/questions/2422/yield-on-www-or-assetbundlerequest-or-both
i managed to prevent a lag when loading some audio with this (needs Unity pro for making asset bundle)
var Audiosrc : AudioSource; Audiosrc = GetComponent(AudioSource); var req : AssetBundleRequest;
function Start () {
var www = new WWW ("http://path_to_Unity_assetbundle/tortoise02.unity3d"); yield www;
req = www.assetBundle.LoadAsync ("tortoise02", AudioClip); yield req;
//Audiosrc.clip = www.assetBundle.LoadAsync ("tortoise02", AudioClip).asset; Audiosrc.clip = req.asset;
if(!Audiosrc.isPlaying && Audiosrc.clip.isReadyToPlay){ Audiosrc.Play();
} }
Answer by parahunter · May 18, 2011 at 06:05 AM
The end solution proved to be to place the sound in the scene being loaded and only switch over to it when it had been completely loaded.
Answer by poncho · May 17, 2011 at 01:27 PM
why dont you have that sound on client side, to avoid the Download, and avoid the bandwidth used for the download, i think that could help