SoundEffect and SoundEffectInstance classes: Performance does matter

An important lesson from using the SoundEffect and the SoundEffectInstance classes is how you load the wav files.

The following code takes a while to run, so you shouldn't run it every time you want to play the same effect. Instead, load it once and only once:

 

SoundEffect effect= SoundEffect.FromStream(TitleContainer.OpenStream("effect.wav"));

 

Then, whenever you want to play it, all you have to do is creating an instance:

 

SoundEffectInstance soundEffectInstance = effect.CreateInstance();

soundEffectInstance.IsLooped = false;

soundEffectInstance.Play();

 

If you are doing this within a game or something where the audio response has to be very quick, it will make the whole difference.