Shawn beat me to the punch and did this great post on the changes to the SoundEffect API in XNA GS 3.1. Here’re some tips if you’re trying to migrate your 3.0 code to the new release.
“Fire and Forget” Playback
// this will play a 2D fire and forget sound effect with default// volume, pitch and panSoundEffect effect = Content.Load<SoundEffect>(“GameSoundEffect”);bool effectPlayed = effect.Play(); // This will play a 2D fire and forget sound effect with specified volume, pitch and panbool effectPlayed = effect.Play(0.8f, 1.0f, 0.0f);
// this will play a 2D fire and forget sound effect with default
// volume, pitch and pan
SoundEffect effect = Content.Load<SoundEffect>(“GameSoundEffect”);
bool effectPlayed = effect.Play();
// This will play a 2D fire and forget sound effect with specified volume, pitch and pan
bool effectPlayed = effect.Play(0.8f, 1.0f, 0.0f);
“Create, Configure and Play”
// game controls the lifetime of the sound effect instance
SoundEffect effect = Content.Load< SoundEffect>(“GameSoundEffect”);
SoundEffectInstance instance = effect.CreateInstance();
instance.Play();
…
instance.Dispose();
// this will play a 2D non-looping sound effectSoundEffect effect = Content.Load<SoundEffect>(“GameSoundEffect”);SoundEffectInstance instance = effect.CreateInstance();instance.Play();
// this will play a 2D non-looping sound effect
// this will play a 3D positioned non-looping sound effectSoundEffect effect = Content.Load<SoundEffect>(“GameSoundEffect”);SoundEffectInstance instance = effect.CreateInstance();instance.Apply3D(listener, emitter);instance.Play();
// this will play a 3D positioned non-looping sound effect
instance.Apply3D(listener, emitter);
// this will play a 2D looping sound effectSoundEffect effect = Content.Load<SoundEffect>(“GameSoundEffect”);SoundEffectInstance instance = effect.CreateInstance();instance.IsLooped = true;instance.Play();// this will play a 3D positioned looping sound effectSoundEffect effect = Content.Load<SoundEffect>(“GameSoundEffect”);SoundEffectInstance instance = effect.CreateInstance();instance.IsLooped = true;instance.Apply3D(listener, emitter);instance.Play();
// this will play a 2D looping sound effect
instance.IsLooped = true;
// this will play a 3D positioned looping sound effect
// this will play a 2D positioned looping sound effect.// setting the Pan in this case overrides Apply3D.SoundEffect effect = Content.Load<SoundEffect>(“GameSoundEffect”);SoundEffectInstance instance = effect.CreateInstance();instance.IsLooped = true;instance.Apply3D(listener, emitter);instance.Pan = 1.0f;instance.Play();
// this will play a 2D positioned looping sound effect.
// setting the Pan in this case overrides Apply3D.
instance.Pan = 1.0f;
Helper Extension Class
Here’s a helper class that may ease the pain of moving your code to the new world order. Remember though, this is a helper for “Create and Configure” playback so you still need to explicitly dispose the instance when you’re done.
public static class SoundEffectExtensions
{
public static SoundEffectInstance Play3D(this SoundEffect effect, AudioListener listener, AudioEmitter emitter)
return Play3D(effect, listener, emitter, 1f, 0f, false);
}
public static SoundEffectInstance Play3D(this SoundEffect effect, AudioListener listener, AudioEmitter emitter, float volume, float pitch, bool loop)
return Play3D(effect, new AudioListener[] { listener }, emitter, volume, pitch, loop);
public static SoundEffectInstance Play3D(this SoundEffect effect, AudioListener [] listeners, AudioEmitter emitter, float volume, float pitch, bool loop)
instance.Volume = volume;
instance.Pitch = pitch;
instance.IsLooped = loop;
instance.Apply3D(listeners, emitter);
return instance;
public static SoundEffectInstance Play2D(this SoundEffect effect)
return Play2D(effect, 1f, 0f, 0f, false);
public static SoundEffectInstance Play2D(this SoundEffect effect, float volume)
return Play2D(effect, volume, 0f, 0f, false);
public static SoundEffectInstance Play2D(this SoundEffect effect, float volume, float pitch, float pan, bool loop)
instance.Pan = pan;