Playing music with multiple parts

How can I set up my game to play music using multiple sound files?

For a simple example, let’s say I have a music track that has an introduction and a looping part. Looping the second part is easy, as the audio elements in Unity can do that flawlessly. But how do I get my game to play a second music track immediately after a first intro track just as flawlessly?

If I write code that waits until the first track stops playing before playing the second track, then there will be a whole frame where there is no audio; a very sudden and apparent blip in the music.
If I try to manually start the second track at the exact moment the first track stops, not only do I have to set up each individual song to know its exact length, but I still can potentially run into the same problem. If the code runs just a little late I’ll have another gap in the audio, and it becomes more likely on slower machines.

So, how else could I do this? How else can I get a specified audio track to play immediately after another, without missing even a sample between them?
Or perhaps a method to have a track loop back to a specified time index of an audio file, instead of back to the very beginning?
Or really just any method that will let me play more music than just a single exactly looping sound file.

So… There is no method then?

I’m thinking the smartest thing might be to have an option to have tracks loop from an optional point instead of just the beginning. I saw a game (not made with Unity) that placed a single marker in each of its music tracks. From the marker to the end of the track formed a perfect loop, but it still had the introduction part that would play first.
The marker was saved into the sound file itself, so nothing had to be set up externally. The game just automatically looped the track from the marker.

That’s what we need in Unity.

Unitys audio syshtem is kind of old → A lot new audio stuff in unity 5

You could use AudioSource.PlayScheduled() to implement gap-less audio in you game.

Check if the current audio playing is less than 1 second (arbitrary) from finishing.
Schedule the next audio to play in the future given how much time the first audio still has remaining.

Can it be that precise?

I will look into this; thank you!

The audio engine runs on a separate thread, so scheduling audio is accurate because it:

  1. doesn’t rely on frames per second
  2. allows the audio to load/decode/buffer audio in advance

As long as you give it enough time to load/decode between the time you schedule it and the time it needs to play, it should play exactly when you schedule it to play.

It’s recommended that you use the timesamples to work out the times for the best accuracy.
You will need two audiosources to create a seamless transition between two clips.

I might be a bit late, but I just made a plugin that precisely does what you wanted for Unity. It is called Introloop and you can take a look of it here : http://forum.unity3d.com/threads/378370/

I uses AudioSource.dspTime and scheduling rather than coroutine, so it is precise and not relying on game time.

This is the idea that I uses as well but my plugins uses 4 AudioSources internally, as it is capable of cross-fading between 2 music track with introduction part, so that is doubled.

As for requirement for using this you need only 1 audio file with intro in it. (Don’t cut them into 2 files!) And 2 time information which is intro boundary (the point where before it, is the intro that you will never heard again) and looping boundary, and the script will do the timing for you.

This plugin will automatically schedule half way from finishing, allowing reasonable loading/preparing time. Because I think in mobile game, 1 second might not enough for big music file.