I have created a simple game for WinRT, and now I want to add sound when the user clicks the buttons.
How can I play the same sound many times? I'm using this code, but it's too slow - sometimes when I click the buttons too fast, the sound is not played.
The class is a Service of MVVM, but if you don't know how to use it, treat it as a static class.
public class SoundService : ISoundService
private SourceVoice VoicePlayer;
private AudioBuffer ClickBuffer;
#region Public Methods
public async void PlayClickSound()
#region Private Methods
private void InitializePlayers()
xaudio = new XAudio2();
masteringVoice = new MasteringVoice(xaudio);
NativeFileStream nativefilestream = new NativeFileStream(
SoundStream soundstream = new SoundStream(nativefilestream);
WaveFormat waveFormat = soundstream.Format;
ClickBuffer = new AudioBuffer
Stream = soundstream.ToDataStream(),
AudioBytes = (int)soundstream.Length,
Flags = BufferFlags.EndOfStream
VoicePlayer = new SourceVoice(xaudio, waveFormat, true);
this.packetInfo = soundstream.DecodedPacketsInfo;
this.playersInitialized = true;
bool playersInitialized = false;