I want to play an intro music clip, followed by another music clip which just plays on loop. How do I go about this?
I’ve added 2 Audio Sources to an object. In my code, when I do a GetComponent(), how do I know which one it has gotten? I could use
AudioSource[] music = GetComponents<AudioSource>();
Now I have an array with both of them, but I still don’t know how to distinguish between them, as they don’t have names in the inspector. I can’t use tags either, because they only apply to the gameobject, not components of the game object.
I’ve removed the Audio Source for the intro from the game object and added the following code:
public AudioClip intro;
void Start () {
PlayMusic();
}
public IEnumerator PlayMusic()
{
Debug.Log("playing intro");
audio.clip = intro;
audio.Play();
yield return new WaitForSeconds(audio.clip.length);
Debug.Log("playing music");
audio.Stop();
AudioSource music = GetComponent<AudioSource>();
music.Play();
}
However, nothing is playing. I have assigned the correct audio clip to ‘intro’ in the inspector. Nothing is being written to the console so it looks like it isn’t even entering the PlayMusic function. This is the first time I’ve used yield, am I doing something wrong?
Ok I have the intro playing now by doing the following:
void Start () {
StartCoroutine(PlayMusic());
}
The console writes “playing intro” and the intro plays. At the end of the intro, the console writes “playing music”. However, then it just plays the intro on loop, instead of playing music on loop. Any idea why?
Do you have any updates on this? I’m trying the exact same thing, and I too get the gap. I’ve tried cutting off the intro before it ends, but that just makes it out of beat with the rest of the music and sounds really awkward.
I have the same issue. I’ve found that audio.clip.length is a float but behaves like an integer, so if your intro clip is 1.5 seconds long, the WaitForSeconds function will wait 2 seconds. Generating a silence of 0.5 seconds and then it plays the loop part. My solution was to hardcode the intro’s length:
yield return new WaitForSeconds(1.5f);
However this does not always work, sometimes it gets stuck, sometimes not… It’s a big problem that unity does not have something like a playlist with events and flags to switch from clip to clip, just like the animation module. If someone finds a better solution, please reply!
Final code:
void Start ()
{
StartCoroutine (PlayMusic ());
}
public IEnumerator PlayMusic ()
{
AudioSource audiosource = GetComponent<AudioSource> ();
audiosource.clip = intro;
audiosource.loop = false;
audiosource.Play ();
yield return new WaitForSeconds (1.5f); //1.5f is the real length in seconds of the intro
audiosource.Stop ();
audiosource.clip = music;
audiosource.loop = true;
audiosource.Play ();
}
This plugin uses only 1 whole music file that already contains the intro, so you don’t have to cut up your music and play one clip followed by another clip like you asked.
It does not uses normal game time nor coroutine like solutions in this thread suggest but uses dedicated AudioSettings.dspTime which I think it is better for the precision.