- Home /
How To Stop An Audio Clip if it's Playing More Than 2 Sounds?
Hey guys, I've been trying to figure out how to stop playing the first audio file if two more start playing while the first is still going.
Because right now, if more than two are playing, I get a very bad white noise effect.
Does anyone know how I can achieve this?
use a timer, roughly 2000.0f is 2 seconds. you an find out how to do it by exact time, but i forgot.
when you play - set a bool to true start timer - i.e. timer--;
when timer
Answer by PouletFrit · May 27, 2014 at 02:48 PM
you will probly need to have something that will count the number of sound playing and that keep tracks of the sounds playing. Each time an audioSource start (or audioClip or whatever ur using), add it to a list and check if that list got bigger than 2, if yes, stop the first sound and remove it from the list.
Also you can either set a timer the length of the sound to remove itself from the list after it have done playing (if ur not looping ur sound) or go thourgh all the sounds of the list to check if they are still playing when ur adding a new one
using UnityEngine;
using System.Collections.Generic;
public class AudioManager : MonoBehaviour {
// The number of maximum sounds to allow playing simultaneously
public int maxSoundPlaying = 2;
private List<AudioSource> soundsPlaying = new List<AudioSource>();
public void PlaySound(AudioSource sound) {
// First remove all the sounds that are done playing
int i = 0;
while (i < soundsPlaying.Count) {
if (soundsPlaying[i].isPlaying) {
i++;
} else {
soundsPlaying.RemoveAt(i);
}
}
// Then start the sound and add it to the list
sound.Play();
soundsPlaying.Add(sound);
// Check if there is too much sound playing,
// if yes remove the first one of the list
if (soundsPlaying.Count > maxSoundPlaying) {
soundsPlaying[0].Stop();
soundsPlaying.RemoveAt(0)
}
}
}
Thanks for the excellent answer! I just tried it but I can still here 3 sounds at once. I just attached this to my main camera that has an audio listener on it. Is that where it's supposed to go? Because all my sounds come directly from objects and are not all in one list.
I have this attached to my objects that have audio on them:
void OnCollisionEnter(Collision collision) {
audio.Play ();
}
I've tried combining your script with this but I think it's not picking up on it because it's sound.Play and not audio.Play
You need to call the function named PlaySound of my script and pass the audioSource containing the sound you wish to play as an argument.
So attach my script to your camera like u did and on your script that you have:
void OnCollisionEnter(Collision collision) {
audio.Play ();
}
replace it by:
public Audio$$anonymous$$anager audio$$anonymous$$anager;
void OnCollisionEnter(Collision collision) {
audio$$anonymous$$anager.PlaySound(this.audio);
}
you should now see a variable named audio manager in ur script, drag and drop the camera with my script attached to it.
It should now work
Hmmm, I just tried to drag the camera into that field but it won't let me, I even tried to just drag that script into it and it wouldn't let me do that either
You attached the Audio$$anonymous$$anager script to the camera and it didnt let u add that script to that field?
Add Component --> Audio $$anonymous$$anager, while the camera game object is selected
Click on the circle next to Audio $$anonymous$$anager on your script that start the audio and select Camera
if it still doesn't work try adding this on ur script that start the audio:
public Audio$$anonymous$$anager audio$$anonymous$$anager;
void Start() {
audio$$anonymous$$anager = Camera.main.GetComponent<Audio$$anonymous$$anager>();
}
as long as u added the audio$$anonymous$$anager to the gameObject containing the main camera it should work