Hooch
Hooch

Reputation: 29693

SoundPlayer: How to select Output device?

How can I select the output device for my application? I'm using the SoundPlayer class to play wav files.

Upvotes: 6

Views: 3970

Answers (2)

tigrou
tigrou

Reputation: 4516

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

Upvotes: 2

Neverbirth
Neverbirth

Reputation: 1042

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.

Upvotes: 6

Related Questions