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.