XNA Game Studio 3.1
How To: Play a Sound
Demonstrates how to play a sound.

To play a sound without using XACT, use the SoundEffect or SoundEffectInstance classes. Using these classes is simpler than using XACT and the classes required by XACT.

Because XACT is not available on Zune, the SoundEffect and SoundEffectInstance will be the only option for Zune games.

SoundEffect and SoundEffectInstance are also available for Xbox and Windows XNA games.

The Complete Sample

The code in this topic shows you the technique. You can download a complete code sample for this topic, including full source code and any additional supporting files required by the sample.

Adding a Sound to Your Project

To add a sound to your project

  1. With your XNA Game Studio game loaded in Visual Studio, right-click the Content icon the Solution Explorer pane.

  2. Click Add, and then click Existing Item.

  3. Navigate to the WAV file you want to play, and then select it.

Simple Sound Playing

Declare SoundEffect.

C#
// Audio objects
SoundEffect soundEffect;
string soundName = "kaboom";

Play the sound.

C#
ContentManager contentManager = new ContentManager(this.Services, @"Content\");
soundEffect = contentManager.Load<SoundEffect>(soundName);
soundEffect.Play();
See Also

Tasks

Reference

Tags :


Page view tracker