The XNA Framework provides audio playback through several core audio classes. It also provides audio classes to developers that use Microsoft Cross-Platform Audio Creation Tool (XACT) to manage multiple sounds.
If your game is to use a few sound files, then the SoundEffect, SoundEffectInstance, and DynamicSoundEffectInstance classes will provide everything you need to play and stream audio during gameplay. For developers that plan on working with multiple sets of sound files during the gameplay, XNA Game Studio provides XACT, an advanced audio content creation and management tool. XACT provides audio designers with the ability to load sound files into groups, to organize those files into discrete cues that can be activated by in-game events, and to create sound transitions. Those using XACT have additional classes that specifically use XACT project data to load and play sounds. For more information about XACT, see Getting Started with XACT.
XACT is not supported on Windows Phone.
Simple Audio Playback
The simplest way to play sounds for background music or sound effects is to use SoundEffect and SoundEffectInstance. Source audio files are added like any other game asset to the project. For example code, see Playing a Sound, Looping a Sound, and Adjusting Pitch and Volume. For background music, see Playing a Song.
Accessing the Audio Buffer
Developers can use DynamicSoundEffectInstance for direct access to an audio buffer. By accessing the audio buffer, developers can manipulate sound, break up large sound files into smaller data chunks, and stream sound. For example code, see Streaming Data from a WAV File.
Both the Cue (XACT) and SoundEffect classes provide the ability to place audio in a 3D space. By creating AudioEmitter and AudioListener objects, both sound playback APIs can position a sound in 3D, and can change the 3D position of a sound during playback. Once you create and initialize AudioEmitter and AudioListener, call Cue.Apply3D or SoundEffectInstance.Apply3D.
Each platform has limits on the number of sounds playing simultaneously. An InstancePlayLimitException exception is thrown if this limit is exceeded. The following table shows the maximum number of simultaneous playing sounds based on platform.
|Platform||Number of sounds that can play simultaneously|
- Getting Started with XACT
Introduces the Microsoft Cross-Platform Audio Creation Tool (XACT).
- Playing a Sound
Demonstrates how to play a simple sound by using SoundEffect.
- Streaming Data from a WAV File
Demonstrates how to stream audio from a wave (.wav) file.
- SoundEffect Class
Provides a loaded sound resource.
- SoundEffectInstance Class
Provides a single playing, paused, or stopped instance of a SoundEffect sound.
- DynamicSoundEffectInstance Class
Provides properties, methods, and events for play back of the audio buffer.