- Home /
How can I prevent AudioClip's from stacking?
Audio stacking is when the same sound is played more than once at the same time. In some cases it will result in increased volume and in other cases, it can cause phasing artifacts. It can happen if more than one script plays the same sound at once. Does Unity provide any tools to prevent stacking?
AudioClip doesn't have a playLimit
property or anything of the sort that I could see. Do I have to write my own audio manager to do this?
Answer by steakPinball · Feb 02, 2012 at 12:15 AM
You can use the isPlaying
property of AudioSource
.
if (!source.isPlaying) {
source.Play();
}
http://unity3d.com/support/documentation/ScriptReference/AudioSource-isPlaying.html
That won't help with multiple AudioSource
's playing the same AudioClip
or with using PlayOneShot
.
Answer by flamy · Feb 02, 2012 at 02:17 PM
you have to actually do same as wat steakpinball told but in a different approach... have all the audio sources in an array and check if any of them plays the particular sound, if not then play this instance!!!!!!
var audioArray:AudioSource;
function Play():boolean
{
for(i=0;i<audioArray.Length;i++)
{
if(audioArray[i].isPlaying)
{
return false;
}
}
source.Play();
return true;
}
i cant find any other easy way arround
Answer by Bryarey · May 15, 2017 at 01:25 AM
Usually I implement a List of clip names, clear it up on every frame, and every time, when I try to play some clip, I check, do this clip`s name contained in List, if not - put this name to list and play clip, else - do nothing.
Your answer
Follow this Question
Related Questions
Mute video player audio in WebGL not working 2 Answers
Ambisonic Audio working on PC but not Android 3 Answers
Making sound names error resistant in an AudioManager 0 Answers
How do I play a sound when pressing the W key, and when I stop pressing the W key, the sound stops? 2 Answers
How can I pre-load samples from an audio file to spawn objects? 1 Answer