- Home /
How can I play one music right after the other ends?
I've many songs that I want randomly play one of it everytime the game starts and then when a sound ends, continue playing and repeating my playlist with a fading between the musics... How can I do this? I was wondering maybe create a GameObject and just attach my 30 songs in it, but I don't think this is the best way to do it...
You gotta call update to check if song is ended.
https://docs.unity3d.com/ScriptReference/AudioSource-isPlaying.html
Answer by tormentoarmagedoom · Jul 05, 2018 at 07:07 AM
Good day.
You need to check in the updte ifthe audiosource is still playing. When is not, you can plsy the next audio clip
Answer by AkshayBisht · Jul 05, 2018 at 10:12 AM
If you want to add fading and other effects, i would recommend using Unity Audio Mixer.
Or you can do this..
adding two game objects to your scene
randomly adding music to their audio source
then decreasing volume of one while increasing volume of other gameobject's audio source.
Answer by Vicente54 · Feb 03 at 09:07 AM
Looking for the same issue. Bumped into your thread. Thanks for creating it. Looking forward for solution.
Answer by MaxLohMusic · Feb 03 at 09:52 AM
The common solution most would recommend is to check isPlaying in the Update() method and play next clip if it's not playing. However there's a very annoying issue: Every time you alt-tab it will consider it "not playing" and start a new piece. So, in addition to that, use the Monobehaviour's OnApplicationFocus(bool b) method. Keep track of it with a variable like
void OnApplicationFocus(bool b) {
Debug.Log("application focus: " + b);
isFocused = b;
}
If isFocused is false, don't play the next clip.
// Update is called once per frame
void Update()
{
if(isFocused && !currentAudio.isPlaying)
playNextMusic();
}
Answer by jdbenitez · Feb 03 at 11:20 AM
Solution one: launch your music in a corroutine and wait async for end;
musicAudio.Play();
yield return new WaitForSeconds(musicAudio.clip.lenght);
nextMusic();
Solution two: launch yout music in a corroutine an wait async while is playing
musicAudio.Play();
yield return new WaitWhile(() => musicAudio.isPlaying );
nextMusic();