- Home /
How many sounds can play all at once?
I have been having the worst trouble with the sound in my game.
I have the start of a race where everyone is reving up their engines. (thats 5 3D sfx)
Then the 321 go! sequence (another 2 2D sfx)
There is the background music playing (1 2D sfx)
There are different ambient city environment sounds looping (3 3D sfx)
There are the regular sfx for the car (5 3D sfx)
Ok, so that's a lot of sound effects, but the priority option goes to 256 layers.. I don't see why 1/4th of my sounds don't play. Occasionally, I can hear this crackling sound on one of the car's sound effects. What is going on? Could it be my sound card, or am I breaking some rules? I first imagined that I could have 256 sounds playing all at once, but where have I gone wrong?
Ok, the crackling was due to a little bad scripting, but I still have the problem of sounds not playing when they should.
Could we see the code you're using? It would help lots to answer your question.
The code is enormous in it's complexity. I would have to share the entire source for you to see what's going on..
But here's the basic code that plays the sound in Update()
if(!BumpSound.isPlaying) { BumpSound.Play(); }
That's super simple and is never supposed to fail.
The problem came when I added the ambient background sounds. They are just plain 3D sound objects that are set to loop and play at start.. nothing complicated about that.
Answer by Rush3fan · Feb 26, 2012 at 11:53 AM
Hey.. I figured it out.
It's a bug in Unity - or with my soundcard.
I forgot to mention one little detail, and that was that I had an audio reverb zone set up in one area of my scene. I deleted it, and everything works now..
Yay.. and Boo that I can't use audio reverb zones!
Well, i guess it depends on how the reverb does work behind the scenes. I guess it can't be applied to many sources at the same time.
Good you figured it out ;)
I'm still working on it.. the doppler effect is another problem. I can drive past a sound source, and will only hear the behind-sound.. and no, it's not because my cars are traveling the speed of sound.. something else weird is going on here.. I might have to test this on more computers, and if the problem persists, I will have to remove the doppler effects all together.
I should also see what happens in a quad speaker scenario.
Oh.. never$$anonymous$$d.. I left it on quad.. that's what happened.. so scratch the doppler issue.
Is it by any chance related to this: Inconsistent AudioSource.volume behavior ?
Because it sounds very similar.
Answer by hawken · Apr 03, 2018 at 03:30 AM
I've found that if you try to play more than 20 sounds at the same time through an audiosource / audiosources unity will stop playing audio entirely.
One technique that is simple is to count how many sounds you are playing and what their duration is, when they stop playing (using clip.length) uncount the sound. If the amount of sounds playing is more than 20 deny any new sounds from playing until less than 20 sounds are playing.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/*
really simple audio manager, to stop unity audio from crackling or becoming inaudible
hawken king 2016
*/
public class AudioPlayer : MonoBehaviour {
public static AudioPlayer instance;
public AudioSource audioSource;
int playing;
void Start()
{
instance = this;
}
public void PlayAudio(string clip)
{
if (playing > 20) return;
StartCoroutine(Playclip(clip));
}
IEnumerator Playclip(string clip)
{
playing++;
AudioClip a = Resources.Load("Audio/"+clip) as AudioClip;
audioSource.PlayOneShot(a);
yield return new WaitForSeconds(a.length);
playing--;
}
}
Answer by Bunny83 · Feb 25, 2012 at 12:45 AM
This depends on your hardware. See the AudioSource.priority documentation:
Unity is virtualizing AudioSources, when there's more AudioSources playing than available hardware channels. The AudioSources with lowest priority (and audibility) is virtualized first. Priority is an integer between 0 and 256. 0=highest priority, 256=lowest priority
Hmm.. there must be a standard number of channels.. I will have to look into this. I really hate my onboard soundcard right now. You can literally 'hear' the graphics card churning out frequencies into my headphones, and then it produces this static noise while my computer is going into hibernation or sleep. It makes me jump out of my chair every time I hear it.. it's so annoying. Anyway.. I'm going to check out the tech specs and see if I can find anything on that. Wish me luck.
"when there's more AudioSources playing than available hardware channels" I don't get that part.. Normally when I am looking at buying a sound card, "channels" is always referred to what type of surround sound it uses. :/
I know that's confusing because they always use "channel". The number of "hardware channels" tell you how many audio sources your soundcard can mix together at once. We hadn't issues in our game so far.
I don't know much about how F$$anonymous$$OD handles multiple sources. Some audio libraries have additional software mixers so they combine multiple sources into one hardware channel, but i'm not sure if Unity / F$$anonymous$$OD supports this. It would of course produce additional load on your CPU.
Hmm.. this is just confusing.. They licence Fmod for like $9,000.. :/ Sounds like a super expensive workaround to me..
Your answer
Follow this Question
Related Questions
Can't get my sound to work.. 1 Answer
2d collision doesn't detect properly in game 0 Answers
Some AudioClips not playing in build 1 Answer
Audio Not Always Playing 3 Answers
Best way to play audio sound effects 1 Answer