Play simple sounds and music

[ This article is for Windows 8.x and Windows Phone 8.x developers writing Windows Runtime apps. If you’re developing for Windows 10, see the latest documentation ]

The WDC DirectX game learning template includes a simple audio class called SoundPlayer. This code demonstrates how to play a sound effect, and how to play music as background audio. You can use this code to experiment with playing media in your game. If you are an audio code developer, you can study this code to learn how sound effects and music can be played with XAudio2.

Note  This topic requires the WDC DirectX game learning template, available for download here.

 

Take note if you are not an audio developer, or if you are an audio developer with little or no experience writing game audio engines: audio management for a production game requires managing many thousands of sound effects, each with dozens, hundreds, or possibly more instances trying to play at one time. The SoundPlayer class is simple because resolving audio priority, managing media resources efficiently, and so on are beyond the scope of this template. By the time your game approaches production quality, you’ll need to see about writing your own audio manager that’s appropriate to your game (or find a third-party solution compatible with Windows Store).

Work with the SoundPlayer sample class

Here’s how you can play a sound right away with SoundPlayer. This code is from ProcessInput in <YourProjectName>Main.cpp. It plays a sound in response to the "fire" input event from InputManager:

case PLAYER_ACTION_TYPES::INPUT_FIRE_PRESSED:
  m_soundPlayer->PlaySound(std::wstring(L"assets/chord.wav"));
  break;

And here’s how to play a music file. Note that the SoundPlayer will play the music as background audio—this is a Windows Store certification requirement. Also, while this plays music in response to the Start button, you’ll want to incorporate this in a way that makes sense — for example, play music on a loop while in a certain game level.

case PLAYER_ACTION_TYPES::INPUT_START:
  m_soundPlayer->PlayMusic(std::wstring(L"assets/sample.wma"));
  break;

That’s probably all that most of you will want to use from SoundPlayer. Those of you who are audio engineers, or who are otherwise interested in learning details about XAudio2 for Windows Store apps should take note of the following: to play sound effects and background audio through XAudio2 at the same time, you need to create two XAudio2 engines. One engine has a mastering voice marked as AudioCategory_GameEffects; this mastering voice is used to play regular game sound effects. The other engine (where your game music is played) has a mastering voice marked as AudioCategory_GameMedia. This allows Windows to mute the game music (and just the music) when the player is using their own media app or receives a voice call.

// Create XAudio2 and a single mastering voice on the default audio device.
UINT32 flags = 0;
HRESULT hr = S_OK;

XAudio2Create(&m_effectAudioEngine, flags);

m_effectAudioEngine->CreateMasteringVoice(
  &m_effectMasteringVoice,    // Pointer to the new IXAudio2MasteringVoice object.
  XAUDIO2_DEFAULT_CHANNELS,   // Number of channels the mastering voice expects in its input audio.
  SOUND_PLAYER_SAMPLE_RATE,   // Sample rate of the input audio data of the mastering voice.
  0,                          // Must be 0.
  nullptr,                    // Use the default audio device.
  nullptr,                    // No effect chain on the mastering voice.
  AudioCategory_GameEffects)
);
    
XAudio2Create(&m_musicAudioEngine, flags);

// This class can play the equivalent of background music, which is tagged 
// on the mastering voice as AudioCategory_GameMedia.
// 
// NOTE TO DEVELOPER: You must categorize game background music properly
// in order to meet Windows Store certification requirements. This
// allows Windows to properly select the background audio the user chooses
// to play when the user runs multiple background audio apps.

m_musicAudioEngine->CreateMasteringVoice(
  &m_musicMasteringVoice,     // Pointer to the new IXAudio2MasteringVoice object.
  XAUDIO2_DEFAULT_CHANNELS,   // Number of channels the mastering voice expects in its input audio.
  SOUND_PLAYER_SAMPLE_RATE,   // Sample rate of the input audio data of the mastering voice.
  0,                          // Must be 0.
  nullptr,                    // Use the default audio device.
  nullptr,                    // No effect chain on the mastering voice.
  AudioCategory_GameMedia
);

Note that if you are incorporating this type of code into a real audio engine, you should put it in an initialization function so that your audio engine can recreate device resources if the audio device is lost or disconnected.

XAudio2

Use the WDC DirectX game learning template