Question about proper usage of SoundEffectInstance

I’m using SoundEffectInstance so that I can have sounds played at varying volume levels depending on distance. I have code similar to this:

static internal void PlaySound(float volume = 1)
        instance = nextSound.CreateInstance();
        instance.Volume = volume;

This method may be called several times a second. What I want to know is—is this creating unnecessary garbage? Do i have to call instance.Dispose() somewhere? I didn’t think I should call Dispose every time PlaySound is called, but I might be wrong.

It would probably be better to not initialize instance every time PlaySound is called, so maybe I should have a List of SoundEffectInstances for each SoundEffect I want to have varying volume.

I’m not certain of the exact behavior but I agree this would be a better way to go.

If you want control over the instance, such as volume, then yes you need to manage your own instances. SoundEffect.CreateInstance() will allocate a new SoundEffectInstance each time it is called, so in your case it would be best to create a pool of them and grab an instance from that pool when you need it. You will also need to return them to the pool when they have finished (instance.State == SoundState.Stopped) so that you can re-use them.

Thanks for the responses. I’ll get around to trying a SoundEffectInstance pool in a while. In the meantime, there’s a gazillion other things I need to work on.