Native DirectX Audio for Windows Store Apps
You can use DirectX to play audio in Store Apps
To play wave files in Mahjong Solitaire for Windows 8 I used SharpDX. When I noticed pops and clicks during animations, I tried a few things like delaying a bit so animations could finish but the audio still did not perform as well as I wanted. Together with Inge Schepers who also experienced some problems with playing waves in her Takuzu game I did something I have not done in 20 years…I wrote C++ code! Luckily we found some WP8 samples we could convert to WinRT.
I wrapped everything together in a demo project you can find on Git.
I you want to use native DirectX audio, you need to do the folowing:
- Copy the XAudioPlayer project and add it to your solution
- Open
XAudioPlayer.cpp
and find theLoadSounds
method
size_t XAudio2SoundPlayer::LoadSounds() {
char16* SOUND_FILE_LIST [] = {
L"Wavs\\match.wav",
L"Wavs\\startup.wav",
nullptr
};
- Add your wavs to SOUND_FILE_LIST.
- Create a Wavs folder in your main project.
- Put the wave files in the Wavs folder
- Copy XPlayer.cs to your main project and play a wav with this code:
private void Button_Tapped(object sender, Windows.UI.Xaml.Input.TappedRoutedEventArgs e) {
XPlayer.Instance.PlayWave(0);
}
In this version you have to add the wavs in the C++ project manually to the SOUND_FILE_LIST. If you can help me improve this code in a way that you can pass the wavs from your main project, we can make it more generic. Let me know if you do!
Happy wave making!