How can I select the output device for my application? I'm using the SoundPlayer
class to play wav files.
You should drop SoundPlayer usage for something like this (and for anything else aside of playing common system sounds). I suggest you go and use NAudio, it allows what you are looking for, and more.
I needed same functionality. Here is my solution using NAudio (same as Neverbirth suggested)
To list all devices:
for (int n = -1; n < WaveOut.DeviceCount; n++)
{
var caps = WaveOut.GetCapabilities(n);
Console.WriteLine($"{n}: {caps.ProductName}");
}
Play wave file:
WaveFileReader wav = new WaveFileReader("somefile.wav");
var output = new WaveOutEvent { DeviceNumber = 0 };
output.Init(wav);
output.Play();
Don't forget to cleanup ressources:
wav.Dispose();
output.Dispose();
More information here and here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With