i have this setup where i have an intro to a background music and the rest of the music as a loop, what i want is to play the intro first then loop the second part indefinitely.
i did a simple script with a coroutine and another that checks if the first part as ended, the thing is that both of them give the same result, the musics stops for like half a second and the second part begins
is there a way to achieve this? one thing though, this is for a mobile game so i’m reduced to one mp3 at a time
This isn’t a problem that can be solved exclusively in code, because the hiccup is a result of the audio system unloading the audio clip that just stopped playing, loading up the new audio clip, then playing it.
What you can do, however, is use a package like Audacity to splice your audio files into a single Master Track. From there, use the
audio.time property to move the play cursor around this master track.
This has other benefits for your game, too, in terms of content value; by recording a couple of different variations on the same theme, you can change the feel of your scene without changing the musical motive.
For instance, consult Final Fantasy XIII-2’s field system. When walking around somewhere, you’ll listen to the normal ambience; when monsters appear, the background music changes to an Aggressive mix, which contains mostly the same melodic material, but has different percussion and supporting parts to give it that ‘aggressive’ feel.
You’re limited to one streaming mp3 at a time. You can have more in memory, they just soak up space. What you could do is have two sources, the first intro with an in-memory mp3, then Play the bgm on another source when it finishes. If you have both on the AudioSources from the start (ie: preloaded), you shouldn’t have to deal with any loading delays, but a dip in framerate could mess you up
In this case I think you’re limited by the engine. I was facing a very similar issue and did lots of reading about on it and that seemed to be the general consensus. (However, in the end didn’t bother trying to implement it, and until the client complains it rots at the bottom of priorities even though it irks me every time I hear it >< I’m interested to see how it turns out!)
Another thing to watch out for is to make your track loop seamlessly. Sometimes you get pops when it jumps from the start to finish if its not set up properly
@kebrus I made a solution called Introloop. It does not use coroutine but uses AudioSettings.dspTime and dedicated audio scheduling methods for its accuracy. You also don’t need to cut your audio into 2 clips, just one with 2 time boundaries on it, and the plugin will play it seamlessly for you.
The memory management is that it will automatically start loading half-way before the previous chunk finish, not right at the baton-passing point. So even on mobile it works smoothly.
Another update 2 years later, not sure when it was added, but I accomplished this using two AudioSource’s, intro and source, and PlayDelayed like so:
float introLength = intro.clip.length;
I originally had one music file and I just chopped it at the loop point using a free wav editor (Audacity)… I have turned the volume way up and I don’t hear the break at all. In case that helps anyone still trying to do this.