Export (0) Print
Expand All
20 out of 32 rated this helpful - Rate this topic

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

  1. Load your XNA Game Studio game in Visual Studio.

  2. In Solution Explorer, right-click the solution's Content project.

  3. Click Add, and then click Existing Item.

  4. Navigate to the .wav file you want to add, and then select it.

    Bb195053.note(en-us,XNAGameStudio.41).gifNote

    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

  1. Declare a SoundEffect object to hold the sound file.

    // Audio objects
    SoundEffect soundEffect;
    
  2. Load the sound file using Content.Load.

    soundEffect = Content.Load<SoundEffect>("kaboom");
    
  3. 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.
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.