List<SoundEffectInstance> explosionSoundInstanceList;
explosionSound = Content.Load<SoundEffect>("sound/explosion");
explosionSoundInstanceList = new List<SoundEffectInstance>(MAX_EXPLOSIONS);
for (int i = 0; i < MAX_EXPLOSIONS; i++)
{
explosionSoundInstanceList.Add(explosionSound.CreateInstance());
}
private void PlayExplosionSound(float volume, float pitch, float pan)
{
ClampSoundValues(ref volume, ref pitch, ref pan);
for (int i = 0; i < explosionSoundInstanceList.Count; i++)
{
if (explosionSoundInstanceList[i].State == SoundState.Stopped)
{
explosionSoundInstanceList[i].Volume = volume / (i+1);
explosionSoundInstanceList[i].Pitch = pitch;
explosionSoundInstanceList[i].Pan = pan;
explosionSoundInstanceList[i].Play();
return;
}
}
return;
}
Works okay.
Zero garbage on the 360 after instantiation.
Will just elegantly fail after you hit the limit.
That's what I use.
I clamp the values thusly
private static void ClampSoundValues(ref float volume, ref float pitch, ref float pan)
{
volume = MathHelper.Clamp(volume, -1f, 1f);
pitch = MathHelper.Clamp(pitch, -1f, 1f);
pan = MathHelper.Clamp(pan, -1f, 1f);
}
As setting a foolish value will drop an exception.