Playing a Sound
This topic demonstrates how to play a simple sound by using SoundEffect. To play a sound using wave banks and sound banks from a Microsoft Cross-Platform Audio Creation Tool (XACT) project, see Playing Sounds from an XACT Project.
Complete Sample
The code in this topic shows you the technique for playing sound. You can download a complete code sample for this topic, including full source code and any additional supporting files required by the sample.
To add a sound file to your project
Load your XNA Game Studio game in Visual Studio.
In Solution Explorer, right-click the solution's Content project.
Click Add, and then click Existing Item.
Navigate to the .wav file you want to add, and then select it.
Note
The selected .wav file is inserted into your project. By default, it is processed by the Content Pipeline, and built wave files are accessed automatically when the game is built.
To play a simple sound
Declare a SoundEffect object to hold the sound file.
// Audio objects SoundEffect soundEffect;
Load the sound file using Content.Load.
soundEffect = Content.Load<SoundEffect>("kaboom");
Play the sound.
// Play the sound soundEffect.Play();
Concepts
- Looping a Sound
Demonstrates how to loop a sound. - Playing Sounds from an XACT Project
Demonstrates how to initialize the audio engine, to load sound banks and wave banks created using XACT, and then to play a sound by using a Cue object.
Reference
- SoundEffect Class
Provides a loaded sound resource. - SoundEffectInstance Class
Provides a single playing, paused, or stopped instance of a SoundEffect sound.