- Home /
Sound Complete c#
How i can detect if a sound have finished playing , so i can release the users actions?
Answer by aldonaletto · May 06, 2012 at 04:38 PM
If you use audio.Play(), you can check its completion with audio.isPlaying - it's true until the sound finishes. The example function below starts playing the sound and returns true; if called while the sound is playing, it returns false and does nothing:
bool PlayClip(AudioClip sound){
if (audio.isPlaying) return false;
audio.clip = sound;
audio.Play();
return true;
}
NOTE: Be aware that other alternatives like audio.PlayOneShot and AudioSource.PlayClipAtPoint don't affect isPlaying!
When using those two functions, you can use a coroutine with a WaitForSeconds( clip.length )
That's a good alternative that works for Play, PlayOneShot and PlayClipAtPoint. But remember that WaitForSeconds can only be used to pause coroutines - the coroutine that uses yield return new WaitForSeconds(time) will pause for the specified time. Coroutines run automatically in the background, thus the code that started the coroutine will not even notice the pause (StartCoroutine returns almost immediately - on the first yield, to be more precise).
Your answer
Follow this Question
Related Questions
Audio Needs to stop 1 Answer
Audio Playing upon death 1 Answer
How do I add sound to a sliding door animation? 2 Answers
PlayOneShot returns false for isPlaying 5 Answers
Collide sound 2 Answers