- Home /
How to play multiple sounds simultaneously
I am creating a 3d endless runner game. I am unable to play multiple sounds at the same time. What I want is to play a sound in the background through out the game that will be stopped when the game overs. When the Horse Rider jumps then play a sound for jump once, When the horse rider changes the lanes then play a sound for changing the lane. When the horse moves forward play a sound for the footsteps as well. Now here i am confused with the audio listeners that how many audio listeners will I be needing? how to play multiple sounds at the same time. Please guide me about this.
Following is my code :
public AudioClip mysound;
private AudioSource source;
public AudioClip collision_sound;
void update ()
{
if (!source.isPlaying) {
source.clip = mysound;
source.Play ();
}
}
public void OnCollisionEnter(Collision collision) {
source.Stop();
source.clip=collision_sound;
source.Play();
}
After this the background sound continues to play...
Answer by georgeq · Jun 01, 2015 at 11:55 PM
You only need one Audio Listener, what you need is multiple AudioSources. You can play only one sound at a time per AudioSource, in order to play multiple sounds you need one AudioSource for each sound you want to play at the same time. You can have multiple GameObjects each one with its own AudioSource o have multiple AudioSources in one GameObject.
Actually, I would like to correct this incorrect answer. It's possible to play multiple sounds at once with ONE Audiosource. You can play up to 10-12 audio sources at once (only) by using PlayOneShot(); With it, Unity mix the audio output from the audio clip into a single channel. (Which is why it's limited to 10-12 clip at once. $$anonymous$$ore and the audio chip of the player's PC or smartphone might not be able to play all the variations.) PlayOneShot(); ignore the "loop" and "mute" function. Using multiple AudioSource create multiple channel for the sounds. If they all come from the same source (like a menu, voices or like gunshots, etc.) you just have to use one Audiosource.
If you want to mix Background musics or other elements that might requires loop or different levels of volumes and pitch (or a different distance obviously), then you must use multiple Audiosource. When multiple sounds are used at once through PlayOneShot();, they all share the same output channel... meaning their volume, distance, spatial blend, Stereo Pan, etc. is shared. Should you drop its output volume, the whole set of sounds played through it will be affected.
That's why I tend to use 1 Audiosource for BG$$anonymous$$, 1 Audiosource to Voices and 1 Audiosource for Voices that are play in 2D space (where distance doesn't affect the volume) and then use 1 Audiosource per character and impact-based object (such as bullets or explosive barrels, etc).
This is an unbelievably good answer, @$$anonymous$$ax_Bol , thank you VERY much. I couldn't find this precise information anywhere. So despite realising I could play multiple sounds at once with one audio source, via PlayOneShot, I knew nothing more about this capacity. The subsequent architecture description makes perfect sense. THAN$$anonymous$$ YOU!!!
$$anonymous$$ax_Bol, you should make that another answer rather than just a comment.
Answer by ldesmet · Dec 08, 2017 at 10:55 PM
Hi everyone !
I've quite an issue very similar. I want to play several frequencies that I compute. For instance I want to play 150 Hz, 200Hz, and 322Hz at once. Playing a single frequency is very easy thank's to this tutorial :
http://www.develop-online.net/tools-and-tech/procedural-audio-with-unity/0117433
Unfortunately, this tutorial doesn't work with multiple frequencies because we have to get several audio listenner which is impossible.
Have you any ideas doing this ?
Thank you :)
If you still haven't figured this out and don't want to use multiple audiosources, I believe that you can simply sum all of the frequencies to get one integer, and then play that frequency.
Your answer
Follow this Question
Related Questions
Audio won't play sound 2 Answers
Audio: -3db automatic attenuation on any audio playing? 0 Answers
How to Switch The Audio Clip using button 0 Answers