我想使用Naudio在C#中以编程方式为我的声卡选择一个特定的采样率。
我的输出是排他模式下的WasapiOut。

我已经尝试了很多东西,但没有任何效果,我到处搜索了,只发现了这个:How to Change Speaker Configuration in Windows in C#?
但是他们并没有真正找到正确的解决方案。

这是我的WasapiOut:

var enumerator = new MMDeviceEnumerator();
MMDevice device = enumerator.EnumerateAudioEndPoints(DataFlow.Render, DeviceState.Active).FirstOrDefault(d => d.DeviceFriendlyName == name);
outputDevice = new WasapiOut(device, AudioClientShareMode.Exclusive, false,200);

我不明白的是在这里:
https://github.com/naudio/NAudio/blob/master/Docs/WasapiOut.md
它说:
“如果选择AudioClientShareMode.Exclusive,则要求对声卡进行独占访问。此方法的好处是您可以指定所需的确切采样率”
而且我在任何地方都找不到如何指定采样率的方法。

如果有人知道答案,那就太好了,谢谢!

编辑:

我想我找到了一种方法:
var waveFormat5 = WaveFormat.CreateIeeeFloatWaveFormat(Int32.Parse(comboBox1.Text), 2);
            var test2 = new MixingSampleProvider(waveFormat5);
            var audioFile = new AudioFileReader("test.wav");
            var input = audioFile;
            test2.ReadFully = true;
            test2.AddMixerInput(new AutoDisposeFileReader(input,waveFormat5));
            outputDevice.Init(test2);

使用“outputDevice”作为我的WasapiOut。
因此,我将ouputDevice采样率设置为我与Mixing Sample Provider所选择的采样率,然后将音频文件发送到该Mixer,这是正确的方法吗?
因为我的音频文件采样率是44100,所以我选择将outputDevice采样率也设置为44100,但是当我制作outputDevice.Play()时,我听到的声音比原始声音快。

最佳答案

创建WasapiOut实例后,您将调用Init传递要播放的音频。假设声卡支持,它将尝试直接使用该音频的采样率(和WaveFormat)。乌西

关于c# - 使用WasapiOut Naudio更改声卡的采样率,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55969745/

10-12 01:11