Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SoundPlayer: How to select Output device?

Tags:

c#

.net

audio

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

like image 866
Hooch Avatar asked May 30 '11 16:05

Hooch


2 Answers

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.

like image 158
Neverbirth Avatar answered Nov 11 '22 17:11

Neverbirth


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

like image 34
tigrou Avatar answered Nov 11 '22 16:11

tigrou