【发布时间】:2014-05-06 11:10:36
【问题描述】:
我的问题与此处类似: Play audio to two different Audiodevices simultaneously with Naudio
但是我在这里再问一次,因为上面的链接中没有明确回答。
我也有它: Play sound in both speaker and headset wpf
灵感来自: Play a sound in a specific device with 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();
使用上面的代码,我仍然只能在一台设备(耳机或扬声器)中听到铃声。
【问题讨论】:
标签: c# wpf naudio speaker headphones