Question by
unity_A48jtrnvGh-YSg · Feb 19, 2020 at 11:44 AM ·
coroutines
How to stop audio in coroutine unity
I've created a method shown below to play audio in coroutine.
public IEnumerator playAudio(int sound)
{
audioSource.Stop();
audioSource.clip = audioClips[sound];
audioSource.Play();
Debug.Log("playing audio: " + audioClips[sound] + "length: " + audioSource.clip.length);
yield return new WaitForSeconds(audioSource.clip.length);
}
public IEnumerator playAudio(int sound1,int sound2)
{
audioSource.Stop();
audioSource.clip = audioClips[sound1];
audioSource.Play();
Debug.Log("playing audio: " + audioClips[sound1] + "length: " + audioSource.clip.length);
yield return new WaitForSeconds(audioSource.clip.length);
StartCoroutine(playAudio(sound2));
}
public IEnumerator playAudio(int sound1, int sound2,int sound3)
{
audioSource.Stop();
audioSource.clip = audioClips[sound1];
audioSource.Play();
Debug.Log("playing audio: " + audioClips[sound1] + "length: " + audioSource.clip.length);
yield return new WaitForSeconds(audioSource.clip.length);
StartCoroutine(playAudio(sound2,sound3));
}
public IEnumerator playAudio(int sound1, int sound2, int sound3,int sound4)
{
audioSource.Stop();
audioSource.clip = audioClips[sound1];
audioSource.Play();
Debug.Log("playing audio: " + audioClips[sound1] + "length: " + audioSource.clip.length);
yield return new WaitForSeconds(audioSource.clip.length);
StartCoroutine(playAudio(sound2, sound3, sound4));
}
here, I'm calling a sequence of audio
StartCoroutine(playAudio(18, 21, 22));
my question is when I call another coroutine while previous sequence is playing, I want to stop the previous audio and play current audio. But, the sequence start playing again after current audio.
Comment