Can not set output device, only use system default output device:
private System.Media.SoundPlayer sp = new SoundPlayer();
sp.SoundLocation = "Resources\\sounds\\speaker.wav";
sp.Load();
sp.Play();
//can set selected device,
private WaveOutEvent _waveOut = null;
private void Test_Loudspeaker_Click(object sender, RoutedEventArgs e)
{
//init
_waveOut = new WaveOutEvent();
//set output device
detectDevice();
//sound file
string filePath = "Resources\\sounds\\test.wav";
WaveFileReader reader = new WaveFileReader(filePath);
_waveOut.Init(reader);
_waveOut.Play();
}
private void detectDevice()
{
string selectedSpeaker = GetCurrentSelectedSpeaker();
if(selectedSpeaker != null && !selectedSpeaker.Equals(""))
{
int waveOutDevices = WaveOut.DeviceCount;
for (int i = 0; i < WaveOut.DeviceCount; i++)
{
var cap = WaveOut.GetCapabilities(i);
if (selectedSpeaker.Contains(cap.ProductName))
{
_waveOut.DeviceNumber = i;
return;
}
}
}
}
private void disposeWave()
{
if(_waveOut != null)
{
if(_waveOut.PlaybackState == NAudio.Wave.PlaybackState.Playing)
{
_waveOut.Stop();
_waveOut.Dispose();
_waveOut = null;
}
}
}
reference: https://github.com/naudio/NAudio/wiki/Playing-an-Audio-File