- Home /
Play Audio in Queue
Hey guys,
I have a single Object which has an Audio Source due to only wanting a single audio to play at a time. Besides that i have various interactables which can be clicked in order to play a sound, meaning clicking an object will tell the game manager which sound he should play... this all works fine so far, however when playing two sounds quickly after another only the second sound gets played, which makes sense since only a single Audio Source is present I assume.
What I am looking for is a way to queue Audio files in the game Manager and then play these in a row one after another...
I guess whenever I tap an interactable, I would need to add its audio clip to a list, the game manager than plays through the list and deletes the clip from the list once it got played correct ?
But how would you do this ? Using corountines to determine if the audio source is still playing, or rather use "PlayScheduled" ?
Would be really greatful for some hints and thoughts.
Answer by Ymrasu · Sep 01, 2020 at 03:48 PM
For me, I would have a "SoundManager" script. So I can just use a PlaySound(clip);
method whenever I need to. So the SoundManger could look something like this:
public class SoundManager : Monobehaviour
{
public AudioSource audioSource;
Queue<AudioClip> clipQueue;
void Update()
{
if (audioSource.isPlaying == false && clipQueue.Count > 0) {
audioSource.clip = clipQueue.Dequeue();
audioSource.Play();
}
}
public void PlaySound(AudioClip clip)
{
clipQueue.Enqueue(clip);
}
}
Your answer
Follow this Question
Related Questions
Multiple PlayOneShot bug 1 Answer
Loading Audio from Asset Bundles not working in Unity 5.2 2 Answers
Does enabling and disabling an AudioSource cause the sound to play, if playOnAwake is enabled? 1 Answer
AudioClips automatically fastforwarding at beginning of scenes 0 Answers
Why do AudioClips still play with no Audiolistener? 0 Answers