Sound Playback

Must Read

Just post!

As an alternative to playing sounds using the media controls described in the section “Media” earlier in this chapter, you can use the XNA interop feature to initiate sound playback. This uses two of the XNA framework classes: SoundEffect and SoundEffectInstance.

To play sounds using the XNA framework classes, you must do the following:

  • Add a reference to the assembly Microsoft.Xna.Framework to your project.
  • Get a reference to an instance of the SoundEffect class.
  • Use the SoundEffect to create a SoundEffectInstance.
  • Call the Play method of the SoundEffectInstance.

The simplest approach is to create a SoundEffect instance and load it with a stream that points to a valid .wav file, then call the Play method of the SoundEffectInstance returned from the Create Instance method. The following code example loads a .wav file from isolated storage on the device and plays it once. To use this code, you must reference the namespaces System.IO and System.IO. IsolatedStorage.

IsolatedStorageFile myStore =
Stream dat = myStore.OpenFile(“MyWavFile.wav”, FileMode.Open,
SoundEffect effect = SoundEffect.FromStream(dat);

If you want to repeat the sound, apply transformations, or specify the volume setting, you can create a SoundEffectInstance and set its properties before calling the Play method.

SoundEffect effect = SoundEffect.FromStream(dat);
SoundEffectInstance sei = effect.CreateInstance();
sei.IsLooped = true;
sei.Pan = (float)0.5;
sei.Pitch = (float)-0.3;
sei.Volume = (float)0.6;


- Advertisement -

Latest News

Yadea Becomes Official Supplier for Hangzhou Asian Games

Yadea Named Official Supplier for Hangzhou 2023 Asian Games, Hosts Fun Run Events in Sri Lanka, Laos, and Vietnam Yadea,...
- Advertisement -

More Articles Like This

- Advertisement -