怎样才能用c#列出播放声音的音频设备?

0 人关注

我正试图用c# WPF做一个应用程序,我需要列出播放声音的音频设备。我是c#的新手,所以你能更详细地解释吗?

c#
wpf
audio
yiyitK
yiyitK
发布于 2021-05-07
1 个回答
darkhac
darkhac
发布于 2021-05-07
0 人赞同

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