SoundEffect changes in XNA Game Studio 3.1

Originally posted to Shawn Hargreaves Blog on MSDN, Friday, June 12, 2009

One of the few breaking API changes between Game Studio 3.0 and 3.1 concerns the SoundEffect.Play method.

There are two different scenarios for playing sound effects:

We think it is important for the SoundEffect API to support both scenarios, and in Game Studio 3.0, we tried to make the SoundEffect.Play method handle them both. It returned a SoundEffectInstance object, which you could use for create & configure scenarios, but you could also just ignore this return value if you were doing fire & forget, in which case we did some magic to make sure the garbage collector would not try to reclaim the SoundEffectInstance while the sound was still playing.

Turns out, this didn't work as well as we expected. There were two problems:

In Game Studio 3.1, we changed the API to make these two usage scenarios explicit:

We removed the SoundEffect.Play3D method. To play a 3D sound, call SoundEffect.CreateInstance, SoundEffectInstance.Apply3D, and then SoundEffectInstance.Play. You cannot use fire & forget with 3D sounds.

We also changed what happens if you try to play too many sounds at the same time. In GS 3.0, the Play call would throw InstancePlayLimitException. In 3.1, CreateInstance still throws this exception if it runs out of voices, but Play just returns false. This is convenient for the common case where you are playing fire & forget sounds and want to silently ignore voice overflows, as you can simply ignore the Play return value and no longer have to write special error handling code.

Blog index   -   Back to my homepage