我正在尝试在C#中使用WASAPI,但我甚至无法在Visual Studio中找到要引用的dll。我应该在COM程序集中引用一个DLL还是从Microsoft网站下载并引用它?有没有关于如何在C#中使用WASAPI的文档?
我想用它来设置麦克风提升等级。我一直在使用NAudio,但在Windows 8.1中它无法正常运行see this。它通过winmm调用设置提升级别。我以为我可以直接使用WASAPI。
修改
我尝试过CSCore,它有一个用于设置麦克风增强的WASAPI调用的包装器。它成功设置了值,但在设置值后,程序每次都会因访问冲突异常而崩溃。以下是CSCore的代码:
MMDeviceEnumerator deviceEnumerator = new MMDeviceEnumerator();
MMDeviceCollection deviceCollection = deviceEnumerator.EnumerateAudioEndPoints(EDataFlow.eCapture, DEVICE_STATE.DEVICE_STATE_ACTIVE);
MMDevice microphone = null;
for (int i = 0; i < deviceCollection.Count; i++)
{
MMDevice device = deviceCollection[i];
if (device.FriendlyName.Contains("Plantronics"))
{
microphone = device;
}
}
if (microphone != null && microphone.AudioSessionManager2.Sessions.Count < 1)
{
return;
}
AudioSessionControl2 activeSession = null;
for (int i = 0; i < microphone.AudioSessionManager2.Sessions.Count; i++)
{
if (microphone.AudioSessionManager2.Sessions[i].State == AudioSessionState.AudioSessionStateActive)
{
activeSession = microphone.AudioSessionManager2.Sessions[i];
}
}
if (activeSession == null)
{
return;
}
activeSession.SimpleAudioVolume.MasterVolume += 0.1f;
我也发现AudioSwitcher库似乎也有一个包装器,但麦克风音量无法改变。它总是-1。以下是AudioSwitcher的代码:
CoreAudioController audioController = new CoreAudioController();
var devices = audioController.GetCaptureDevices(DeviceState.Active);
foreach (CoreAudioDevice device in devices)
{
if (device.FullName.Contains("Plantronics"))
{
device.Volume = 49;
}
}