我的问题与这里的问题类似: Play audio to two different Audiodevices 同时与 Naudio
但是我在这里再次问它,因为上面的链接中没有明确回答。
我也有它: 在扬声器和耳机中播放声音 wpf
灵感来自: 使用 C# 在特定设备中播放声音
我在这里添加源代码并添加 NAudio 标记。
我有一个 wpf 应用程序,我正在使用 soundPlayer 类来播放声音(例如铃声)。目前,该音调在扬声器或耳机上播放(如果已插入)。即使插入耳机,我也希望应用程序在扬声器上播放音调。我知道在 android 中有方法可以做到这一点,但在 wpf 中找不到任何方法。我还想要一个用户界面,让用户可以选择他想听到声音的设备。任何帮助表示赞赏。谢谢 !
public void detectDevices()
{
int waveOutDevices = WaveOut.DeviceCount;
switch (waveOutDevices)
{
case 1:
var wave1 = new WaveOut();
wave1.DeviceNumber = 0;
playSound(0);
break;
case 2:
var wave2 = new WaveOut();
wave2.DeviceNumber = 0;
playSound(0);
var wave3 = new WaveOut();
wave3.DeviceNumber = 1;
playSound(1);
break;
}
}
public void playSound(int deviceNumber)
{
disposeWave();// stop previous sounds before starting
waveReader = new NAudio.Wave.WaveFileReader(fileName);
var waveOut = new NAudio.Wave.WaveOut();
waveOut.DeviceNumber = deviceNumber;
output = waveOut;
output.Init(waveReader);
output.Play();
}
public void disposeWave()
{
if (output != null)
{
if (output.PlaybackState == NAudio.Wave.PlaybackState.Playing)
{
output.Stop();
output.Dispose();
output = null;
}
}
if (wave != null)
{
wave.Dispose();
wave = null;
}
}
case eSelector.startIncomingRinging:
fileName = ("Ring.wav");
detectDevices();
使用上面的代码,我仍然只能在一台设备(耳机或扬声器)中听到铃声。