- Home /
When is audioclip length available ?
I currently load and play audioclips through a coroutine :
IEnumerator loadSoundClip(string fileName)
{
string filePath = Application.streamingAssetsPath + "/Audio/" + fileName + ".wav";
// Start a download of the given URL
WWW request = new WWW("file://" + filePath);
// Wait for download to complete
yield return request;
theAudioClip = request.GetAudioClip(false, false);
theAudioSource = AddAudio(theAudioClip, false, true, 1);
theAudioClipLength = theAudioClip.length;
theAudioSource.Play();
}
But when I do this :
StartCoroutine(loadSoundClip("mySoundClip"));
Debug.Log(theAudioClipLength);
The debug log always returns 0. I suspect this has to do with the update/start/frame mechanism and that I need to wait at least one frame before the variable theAudioClipLength to be populated, before I can retrieve a meaningfull value.
So when trying the following :
StartCoroutine(loadSoundClip("mySoundClip"));
yield return new WaitForSeconds(0.1f);
Debug.Log(theAudioClipLength);
the above works for 90% of the time, but in a few instances, even the 0.1 second delay is not enough.
I can increase the wait time some more, but this seems like a clunky way to go about.
Is there a preffered way to achieve what I am trying to do ?
A related question : is it recommend to also destroy the audiosource when it has finished playing ?
Your answer
![](https://koobas.hobune.stream/wayback/20220612174606im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Destroy the first GameObject out of x fired 1 Answer
Play Sound on Destroy 1 Answer
Play Sound on Destroy 1 Answer
Audio Not Lining Up With Text 1 Answer
Audio won't play on collision 0 Answers