Why does AudioSource cut off beginning of manually looped clip in certain conditions?

I’m dynamically changing the clip of an AudioSource. When I do this I set the time of the audiosource to where it was before I change the clip; my intention is that if I swap out the clip at say 0.5 seconds into the first clip, it will play from 0.5 seconds in the second clip.

That’s fine and works, however I am also looping these sounds. I am looping them manually as they are only to loop under certain conditions. To do this I check if the AudioSource is no longer playing, and if it isn’t I Play() it again.

However when I use this at the same time as swapping out an AudioSource in the middle of a clip and then seeking back to that position after the switch, every time I loop it plays from the seeked position in the middle of the clip!

Am I doing something wrong, or is this a bug, or is it even documented behaviour? (I’ve tried to look in the docs but they seem quite sparse.)

I’ve distilled the behaviour into a small example project available here: http://dl.dropbox.com/u/74853449/SoundTest.zip

If you don’t want to look at that, this is the relevant code:

#pragma strict

var clip1:AudioClip;
var clip2:AudioClip;
var source:AudioSource;

function Start() {
	source.clip = clip1;
	source.Play();
}

function Update() {
	if (!source.isPlaying) {
		source.time = 0; // seems to have no effect
		source.Play();
	}
	
	if (Input.GetKeyDown("space")) {
		var time = source.time;

		// source.Stop(); // makes no difference if this line is commented or not
		source.clip = source.clip == clip1 ? clip2 : clip1;
		source.time = time; // seek to where previous clip left off
		source.Play();
	}
}

What I expect to happen:

  • Audio clip plays and loops from start to end over and over
  • When space is pressed, switches to second clip, which starts playing at the point where first clip left off
  • Second clip now gets to end and then starts again from beginning and loops over and over

What seems to happen:

  • Audio clip plays and loops from start to end over and over
  • When space is pressed, switches to second clip, which starts playing at the point where first clip left off
  • Second clip now gets to end and then starts again but starts from where the first clip left off every time.

Other notes:

  • I am using uncompressed WAVs, not MP3s.
  • Problem doesn’t seem to manifest if I use the loop feature of AudioSource, but as I said my actual project only loops when certain conditions are met, hence the manual looping.
  • Clips are same length.

A few things:

-To be really accurate, use audio.timeSamples and not time. This will give you sample-level accuracy.

-If you have set timeSamples or time to a value greater than 0, every time you ask the audioSource to Play(), it will play from that point. If you set an AudioSource’s loop property to true and it’s timeSamples to something greater than 0, it will start at your defined point, but will loop from the beginning.

-To sync precisely, query timeSamples and use Play(offsetInSamples);

-Better to have 2 AudioSources to sync accurately.