I know this is old, but I wanted to share my IntroLoop class for free!
By default, this class plays the file from the beginning, looping from introBoundary to loopBoundary just like 5argon's. I have added some new features, coding time: 30 minutes.
One, you can play just a loop, no intro. Two, same as default but you can start anywhere not just at the beginning and still have and intro and a loop. and Three, you can play a the file from point a to b without looping by setting IntroLoop.playOnce to false after constructing the intro loop object. So now you can put all your audio into a single file.
example of use:
After attaching your audio to the script's object, drag the audio component to a public field, and send into the constructor, You can make more than one IntroLoop object from the same audio file. This example will play an intro starting at the 5 second mark and loop from the 10 second mark to the 20 second mark:
//Example of use
//Construct
IntroLoop clip = new IntroLoop(audioSource,5f,10f,20f);
//no intro just loop
IntroLoop clip2 = new IntroLoop(audioSource,10f,20f,false);
//you can set it to play once
clip2.playOnce = true;
//call to start
clip.start();
//call once a frame, this resets the loop if the time hits the loop boundary
//or stops playing if playOnce = true
clip.checkTime();
//call to stop
clip.stop();
/* **************************************************** */
//The Music IntroLoop Class handles looping music, and playing an intro to the loop
public class IntroLoop {
private AudioSource source;
private float startBoundary;
private float introBoundary;
private float loopBoundary;
//set to play a clip once
public bool playOnce = false;
//play from start for intro
public IntroLoop(AudioSource source, float introBoundary, float loopBoundary) {
this.source = source;
this.startBoundary = 0;
this.introBoundary = introBoundary;
this.loopBoundary = loopBoundary;
}
//play from start for intro or just loop
public IntroLoop(AudioSource source, float introBoundary, float loopBoundary, bool playIntro) {
this.source = source;
this.startBoundary = playIntro?0:introBoundary;
this.introBoundary = introBoundary;
this.loopBoundary = loopBoundary;
}
//play from startBoundary for intro, then loop
public IntroLoop(AudioSource source, float startBoundary, float introBoundary, float loopBoundary) {
this.source = source;
this.startBoundary = startBoundary;
this.introBoundary = introBoundary;
this.loopBoundary = loopBoundary;
}
//call to start
public void start() { this.source.time = this.startBoundary; this.source.Play(); }
//call every frame
public void checkTime() {
Debug.Log(this.source.time);
if (this.source.time >= this.loopBoundary) {
if (!this.playOnce) { this.source.time = introBoundary; }
}
}
//call to stop
public void stop() { this.source.Stop(); }
}
//The Music IntroLoop Class
/* **************************************************** */