- Home /
Audio cuts out once a certain number of sounds are played
I am having trouble with my audio cutting out once a large number of sounds are played at once, when a lot of enemies are firing, and then resumes after a few seconds. I have a WorldController object that I have attatched a SoundController script to. This script takes some public AudioClip variables and plays them through functions. For example here is the most used one:
//audioCenter is my AudioSource and FriendFire is the AudioClip
public void playFriendFireSound(){
audioCenter.PlayOneShot (FriendFire);
}
This is called by the player whenever it fires a bullet.
I suspect its because an audio source can only handle so much audio but am not sure. Is this the problem and is there another way I can cleanly approach managing audio in my scene?
Thanks guys!
-Lawrence
Bump. This is driving me nuts. So much so that after 2 years I am getting ready to change game engines.
Wow blast from the past. If I recall correctly the problem was that I was simply overloading the audio source with too many sounds playing at once. I had one enemy that was firing at a ridiculous speed (I think it was initially called every frame it was firing) and it was killing the audio. I think I just lowered the fire rate or just stopped it from playing a new sound clip every time it fired.
I know I still have the code and I'll try and find specifics for you.
So here we go! This was my solution. It turns out unity audio sources can get overloaded if too many sounds were played through them. My solution was to create multiple audio sources for specific sounds. That way there does not need to be overlap and every audio center doesn't need to be filled with every sound. This fixed the problem for me and even if an audio source is overwhelmed only that specific audio source will cut out (e.g. if I play too many shooting sounds my music still works even if the shooting sounds cut out) Here is my code:
void Start () {
audioCenter = GetComponent<AudioSource> (); // main sound source used for most things besides firing.
friendFireAudioSource = gameObject.AddComponent<AudioSource> (); //creates a new sound controller for player fire
friendFireAudioSource.clip = FriendFire;
enemyFireAudioSource = gameObject.AddComponent<AudioSource> (); // creates new audio source for enemy fire
enemyFireAudioSource.clip = EnemyFire;
}
Your answer
Follow this Question
Related Questions
What could make the audio loop? 2 Answers
Audio Manager 1 Answer
Is there a way to create a random Audiosource loop? 2 Answers
PlayOneShot returns false for isPlaying 5 Answers
Play sound from an array, not random, and only once 3 Answers