x


Sound Complete c#

How i can detect if a sound have finished playing , so i can release the users actions?

more ▼

asked May 06 '12 at 04:36 PM

albino gravatar image

albino
16 3 3 4

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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!

more ▼

answered May 06 '12 at 04:38 PM

aldonaletto gravatar image

aldonaletto
42.5k 16 43 202

When using those two functions, you can use a coroutine with a WaitForSeconds( clip.length )

May 06 '12 at 04:42 PM Berenger

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).

May 06 '12 at 05:16 PM aldonaletto
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x542
x16

asked: May 06 '12 at 04:36 PM

Seen: 1261 times

Last Updated: May 06 '12 at 05:16 PM