bug with audiosource.timeSamples, it never reaches audio.clip.samples!

I guess it’s a bug, the doc has no mention of corner case.
So then, how do I detect the end of a clip being played in a way that doesn’t get confused when .Pause() is called?

It’s not a bug, source.timeSample turns to 0 when it’s done with the clip and the speed at which it does that falls between frames so Update never gets a change to see timeSample == clip.samples
A solution is this:

public class TestTimeSample : MonoBehaviour {
 [SerializeField] AudioSource source;
 bool playing;
 void OnGUI()
 {
 if (GUILayout.Button ("Play")) {
 source.Play ();
 }
 GUILayout.Label (source.timeSamples.ToString());
 GUILayout.Label (source.clip.samples.ToString());
 if (source.timeSamples > 0 && playing==false) {
 playing = true;
 Debug.Log ("playing");
 }
 if (playing && source.timeSamples == 0) {
 playing = false;
 Debug.Log ("finished");
 }
 }
}
1 Like