如何列出使用c#播放声音的音频设备



我正在尝试用c#WPF制作一个应用程序,我需要列出播放声音的音频设备。我是c#的新员工,你能详细解释一下吗?

使用cscore可以实现

using (var enumerator = new MMDeviceEnumerator())
using (var collection = enumerator.EnumAudioEndpoints(DataFlow.All, DeviceState.All))
// ^^^ take a look on parameters here, you can filter devices with it
{
foreach (var device in collection)
{
//your device using here
//don't forget to dispose device, because it is MMDevice which inherited from IDiposable
}
}

然后让我们想象一下你想检查当前的游戏水平

using (var meter = AudioMeterInformation.FromDevice(device))
{
if(meter.PeakValue > 0)
{
//your code here
}
}

如果您需要通过输出过程过滤输出设备,请查看AudioSession

using (var sessionManager = GetDefaultAudioSessionManager2(DataFlow.Render))
using (var sessionEnumerator = sessionManager.GetSessionEnumerator())
{
foreach (var session in sessionEnumerator)
{
//each session will be assigned to any process
//implement your logic to select preferred
using (var meterInformation = session.QueryInterface<AudioMeterInformation>())
{
var peak = meterInformation.PeakValue;
//here you can check peak value and implement your wishes
}
}
}

但它只适用于Win平台。作者宣布跨平台版本仅作为实验

最新更新