XNA SoundEffect Class

,

Sometimes being a Windows Phone developer means being spoiled for choice. The XNA FCL contains various classes that you can harness in your XAML-based apps, and vice versa. One such class is the SoundEffect class, which is a lightweight class that allows you to specify an audio file and to play it from code without relying on, for example, the MediaElement control.

The SoundEffect class works well for short samples, where instant playback is required. SoundEffect provides support for controlling volume, panning, looping, and pitch of the audio, and even allows you to apply 3D audio effects. Be warned, however, that it is fussy about the format of the audio. I found that only PCM format wav files are supported. I recommend using a tool like GoldWave (www.goldwave.com/) to save all audio to a PCM format. For longer clips, it makes more sense to use a more space efficient format, such as MP3, but for this you need to use the MediaElement control.

To use the SoundEffect class, the Build Action of all audio files that you intend to be played must be set as Content.

A sound effect can be defined in your page code-beside, as demonstrated:

readonly SoundEffect footstepSoundEffect
    = SoundEffect.FromStream(TitleContainer.OpenStream("Audio/Footstep.wav"));

The SoundEffect can then be played like so:

FrameworkDispatcher.Update();
footstepSoundEffect.Play();


Note

Calling FrameworkDispatcher.Update regularly is necessary for the XNA infrastructure to function correctly and for fire and forget sound effects to function correctly. If not called, an InvalidOperationException will be thrown.

Configuring a XAML-based app to work correctly with the XNA framework is discussed in the following section.


Behind the scenes a SoundEffectInstance object is automatically created from the SoundEffect class as soon as the Play method is called. You can create multiple SoundEffectInstance objects and play them from a single SoundEffect. The benefit of using SoundEffectInstances is that these objects share the resources of their parent SoundEffect, and also that a SoundEffect object can be used to control all its SoundEffectInstance sounds. The SoundEffect.MasterVolume property, for example, can be used to modulate the volume of all child SoundEffectInstances.

Use the CreateInstance method of the SoundEffect class, to create a new SoundEffectInstance, as shown:

SoundEffectInstance instance = footStepSoundEffect.CreateInstance();


Note

A maximum of 16 SoundEffectInstance instances can be playing at one time, combined across all loaded SoundEffect objects. Attempting to play more than 16 has no effect.



Note

A SoundEffect continues to hold its memory resources throughout its lifetime. All SoundEffectInstance objects created from a SoundEffect share memory resources. When a SoundEffect object is destroyed, all SoundEffectInstance objects previously created by that SoundEffect will stop playing and become invalid.

Both the SoundEffect class and the SoundEffectInstance class implement IDisposable. When you are finished using an instance of either type, be sure to call its Dispose method to free valuable resources.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.255.44