I have an app that I need to play a wav file when a key is pressed, I use the SoundPlayer class but if another sound is being played when a new key is pressed, it stops the sound the play it again making it look ugly....
is there any way to play the same sound again even if there is another of being played?
thanks!
Use
PlaySound
from the windows API in combination with theSND_ASYNC
andSND_NOSTOP
flags.http://www.pinvoke.net/default.aspx/winmm.playsound
Usage
Declarations
Update
Apologies, you are correct. The API cannot be used to play sounds simultaneously. You should be using the
waveOut
api. Look at this article: http://www.codeproject.com/KB/audio-video/cswavrec.aspxThere's a free (for non-commercial applications) sound library for .NET called Irrklang that supports playing multiple sounds (at least wav and mp3 from what I can gather) at the same time, their tutorial specifically covers this case as basic usage of the library.
If you're asking, "how can I play multiple sounds at once" then SoundPlayer will never be the answer. PlaySound, I believe, is also similarly limited.
You might look at this question and this question for more options on sound APIs. I took a quick look at SDL and SDL Mixer and thought that SDL was too primitive (you have to mix the sounds yourself) and SDL Mixer was too heavyweight (it's all that and a bag of chips - unlimited channels of mixing and music (mp3, ogg, midi, etc)). I couldn't see an online reference for BASS's C# bindings, but it's free for non-commercial use.
NAudio might offer what you need. I haven't used it myself, but it's relatively popular.