Why my Coroutine don t assume the length of my audio clip after stop ?

I have 3 audio clips: startSource with 10s length; loopSource that is a loop of engine and crashSource that is the sound of collision with 2s.

I have one script that should play the audio clip with loop (loopSource) after the first audio clip (startSource) is finished and another audio clip for playing the crashSource sound on trigger event.

If I let length startSource playing until the end, everything works fine.
When I stop the startSource before the end of the length,by the trigger event, the rest of the startSource length and the loopSource clip starts to play in the background, as if the startSource was paused or "WaitForSeconds " did not stop counting when I stop all the audiosources.

Here is my code from first script:

 public AudioClip startClip;
public static AudioSource startSource;
public AudioClip loopClip;
public static AudioSource loopSource;
public AudioClip crashClip;
public static AudioSource crashSource;

private void Start ()
     {
         startSource = gameObject.AddComponent<AudioSource>();
         startSource.playOnAwake = false;
         startSource.rolloffMode = AudioRolloffMode.Linear;
         startSource.loop = false;
         startSource.clip = startClip;
         loopSource = gameObject.AddComponent<AudioSource>();
         loopSource.playOnAwake = false;
         loopSource.rolloffMode = AudioRolloffMode.Linear;
         loopSource.loop = true;
         loopSource.clip = loopClip;
         crashSource = gameObject.AddComponent<AudioSource>();
         crashSource.playOnAwake = false;
         crashSource.rolloffMode = AudioRolloffMode.Linear;
         crashSource.clip = crashClip;

         if(soundOn){
             StartCoroutine(StartSounds());   
         }
}

    void OnTriggerEnter2D(Collider2D other){
        if(GameScreen.gameStart){
            GameScreen.gameStart = false; // game will stop
            if(MenuScreen.soundOn){
                crashSource.Play(); // play crash sound
            }
            startSource.Stop(); // stop engine sound
            loopSource.Stop(); // stop engine sound
            StartCoroutine(ShowGameStop());
        }
    }


    private IEnumerator ShowGameStop() {
        yield return new WaitForSeconds (3.0f);
        GameScreen.gameStart = true;
        bang.SetActive(false);
        playerCar.rectTransform.anchoredPosition = new Vector3 (0, playerPos, 0);
        StartCoroutine (StartSounds ());
    }


    private IEnumerator StartSounds() {
        startSource.Play();
        yield return new WaitForSeconds (startSource.clip.length);
        startSource.Stop();
        loopSource.Play();
    }

The audio file for startSource is mp3 and the loop audio is ogg. Is this a problem ?

All the help is welcome.

That looks like it should work. What does Debug.Log(startSource.clip.length) tell you?

You could also do something like this. Though you may have to wait a frame or so after Play for it to register as playing.

private IEnumerator StartSounds() {
    startSource.Play();
    yield return new WaitUntil(() => !startSource.isPlaying);
    startSource.Stop();
    loopSource.Play();
}
2 Likes

Debug.Log(startSource.clip.length) tells 10.89989 that is the real length of the sound
Tomorrow I will try your sugestion. thanks.

It worked but I had to make some modifications.

I have to add a condition to make it work

private IEnumerator startSounds() {
        startSource.Play();
        yield return new WaitUntil(() => !startSource.isPlaying && GameScreen.gameStart);
        loopSource.Play();
    }

Thanks for this help;)