Question by
bromley · Dec 25, 2017 at 02:53 PM ·
timesoundaudiosourceloop
Loop sound from the middle to the end
I want to play a sound with a loop but I don't want that when the sound reaches the end it starts to the beginning. Is it possible to create a loop from the middle of the sound instead of from the start?
Maybe the answer is using SetScheduledStartTime() and PlayScheduled(), but I don't understand how.
Please help!!!
Comment
Best Answer
Answer by bromley · Dec 26, 2017 at 09:13 AM
Nevermind, I found a solution on my own. If you are interested this is the method:
IEnumerator PlayCustomLoop (AudioSource sound, float endIntro)
{
sound.loop = false;
float l = sound.clip.length;
int t = 0;
sound.Play ();
yield return new WaitForSeconds (endIntro);
t = sound.timeSamples;
yield return new WaitForSeconds (l - endIntro);
LOOP:
sound.timeSamples = t;
sound.Play ();
yield return new WaitForSeconds (l - endIntro);
goto LOOP;
}
Unfortunately you have to know first the value endIntro, namely the time in which the sound has to start the loop. I hope this can help you.
If you are having trouble with it pausing briefly before looping again, make the endIntro variable an int instead of a float.