2
votes

I have a Windows Form Application in Visual Studio using C# and I have a keyboard key sound play whenever a key is pressed using:

keyClick[randomiser.Next(0, 4)].Play();

(The randomiser is just picking from an array of different sounds)

however when the sound is played and another key is pressed before the sound finishes, it stops playing that sound and plays a new one, which makes it sound bad.

So my question is how do I ensure that it plays the entire sound file and just overlays any new play command over the top instead of stopping the first from playing?

1
You could spawn a new thread and play the sound Async. That way it wont be interupted - Cody Popham

1 Answers

3
votes

You could play the sound asynchronously using Async so that it separates the sound and your input. You could also start a Task to do the same thing. Both start the sound on a new thread and you can continue to use input that won't interrupt it.

Code Sample:

Task playSound = new Task(() => keyClick[randomizer.Next(0,4)].Play()); playSound.Start();

Hope this Helps