简体   繁体   English

添加/删除声音设备后,如何在 NAudio 中 select 纠正声音 output 设备?

[英]How to select correct sound output device in the NAudio, after added/removed a sound device?

I am using the NAudio library to test audio output devices on Win based PC.我正在使用 NAudio 库在基于 Win 的 PC 上测试音频 output 设备。 Now, when I launch app, everything works fine, until I add, or remove new audio device - such as stick/remove headphones to the headphones audio jack.现在,当我启动应用程序时,一切正常,直到我添加或移除新的音频设备 - 例如将耳机粘贴/移除耳机音频插孔。 Since this moment on, the static list of devices: var devs = NAudio.Wave.DirectSoundOut.Devices gets updated.从此刻开始,static 设备列表: var devs = NAudio.Wave.DirectSoundOut.Devices得到更新。 I look at the devices names, determine its index - in the IEnumerable devs but when I set the desired device number:我查看设备名称,确定其索引 - 在IEnumerable devs人员中,但是当我设置所需的设备编号时:

_nAudioSoundOut = new NAudio.Wave.WaveOut() { DeviceNumber = wantedDeviceIndex};  

then Init and play:然后初始化并播放:

_nAudioSoundOut.Init(_mp3Reader);    
_nAudioSoundOut.Play();

then some of the devices seem to be confused (swapped).然后一些设备似乎被混淆(交换)。 Is there something I missed, doing wrong, or did I spot a bug?有什么我错过的、做错的,还是我发现了一个错误?

Yes, I made sure, I index proper device by its name, and yes I did consider the -1 to the index, as -1st is the default windows device, and first in the list is actually 0.是的,我确定,我按其名称索引正确的设备,是的,我确实考虑了索引的 -1,因为 -1st 是默认的 windows 设备,列表中的第一个实际上是 0。

Thank you !谢谢 !

The problem was, I used the static device list devs = NAudio.Wave.DirectSoundOut.Devices which is not maintain same order as devices in NAudio.Wave.WaveOut.. Strange and confusing.问题是,我使用了 static 设备列表devs = NAudio.Wave.DirectSoundOut.Devices与 NAudio.Wave.WaveOut 中的设备的顺序不同。奇怪而令人困惑。 Hence, this is what I had to do to get fixed..因此,这是我必须做的才能修复..

for (int idx = 0; idx < NAudio.Wave.WaveOut.DeviceCount; ++idx)
    {
        string devName = NAudio.Wave.WaveOut.GetCapabilities(idx).ProductName;
        if (devName.Contains(SelectedSpeaker.DisplayName) || SelectedSpeaker.DisplayName.Contains(devName))
        {
            indexSelectedSpeaker = idx;
            break;
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM