- Home /
How to create a continous transition between intro and loop of a song
I have this small problem where im implementing this music system, i have separated my song into two parts perfectly and there's no problem when you run them in succession with a audio software....but when i put them into unity....there's this small problem that a noticeable crack appears between the intro and the loop....this is the Coroutine im using..
public IEnumerator MusicFixed(string code, int dial)
{
Song song = Array.Find(music.songs, song => song.code == code);
float volume = setvolume;
MusicChannel channel = channels[dial-1];
channel.intro.Stop();
channel.loop.Stop();
channel.song = song;
channel.intro.clip = song.intro;
channel.intro.volume = volume;
channel.loop.clip = song.loop;
channel.loop.volume = volume;
channel.intro.Play();
yield return new WaitForSeconds(channel.intro.clip.length);
channel.loop.volume = volume;
channel.loop.Play();
}
I have tried using a coroutine that works in sync with Update and whether the Audio source is playing or not....but this only makes the problem worse.....can somebody suggest a solution to this?....i don't want to use any external assets ideally...i know this is a problem with either floating point imprecision, Update not being continous or WaitForSeconds() not being that accurate...EDIT: The Song and MusicChannel are Serializable Classes, with the song having a code and two songs and MusicChannel having a Song and two AudioSources
anyone? i can't find any solution online, there are crossfading tutorials, but none about continuing the same song
@Devster2020 , i once again apologise for calling upon you like this, but this problem is a bit crucial and i can't find a fix for it without using any external asset
Answer by Pangamini · Aug 01, 2021 at 08:52 PM
Music playback is very much time dependent. There is no way how to synchronize frame update ( and coroutines ) with the audio output, simply because the music keeps playing even between the frames. Let's say that you have 120 fps, but the music update frequency is 44000hz. Therefore, in order to control the music from script like that, you have to work on that frequency. Luckily, you have some options
AudioSource.PlayScheduled
AudioSource.PlayDelayed
MonoBehaviour.OnAudioFilterRead
First two options let you schedule a clip playback. They should, according to the docs, work on the dsp thread, with the required precision. I have personally never used either of these methods, but I believe they can be used to solve your problem. The last option lets you manually modify or inject any audio signal to the pipeline. The method is called on the dsp thread and you can use it to write any audio effects or playback scheduling you want. A bit more work perhaps, but gives you full control over what and when you will hear.
Answer by Devster2020 · Aug 02, 2021 at 09:02 AM
Hi dear @rage_co
I'm here hihihi
If I understand correctly, you feel a little pause between songs, right?
If so, it could be that the audio track is slightly heavy and therefore needs a few milliseconds to load .. you could try the following, based on the official docs:
Go into the "Project" view
Find your sounds
Click on them, and in the inspector set as true the following values:
Load in background and Preload Audio Data
Load in background means that the audio clip will be loading in the background without causing stalls on the main thread.
Preload Audio Data means that the audio clip will be pre-loaded when the scene is loaded.
Maybe in this way the sound will starts immediately.
Hope this can help you!
Here is a screenshot regarding what I told you:
I haven't had much time on my pc since a day and a half...and probably will be for the next week or so.....so i will give an update when i try it.....also @Panga$$anonymous$$i 's answer seems promising too.....so thanks again for reaching out to help me
Your answer

Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
StopCourutine not working as expectedd 2 Answers