Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何列出使用c语言播放声音的音频设备#_C#_Wpf_Audio - Fatal编程技术网

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

C# 如何列出使用c语言播放声音的音频设备#,c#,wpf,audio,C#,Wpf,Audio,我正在尝试用c#WPF制作一个应用程序,我需要列出播放声音的音频设备。我是c#的新手,你能更详细地解释一下吗?有了你就可以了 using (var enumerator = new MMDeviceEnumerator()) using (var collection = enumerator.EnumAudioEndpoints(DataFlow.All, DeviceState.All)) // ^^^ take a look on parameters here, you can filt

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

有了你就可以了

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
     }
 }
如果您需要通过输出进程过滤输出设备,请查看

使用(var sessionManager=GetDefaultAudioSessionManager2(DataFlow.Render))
使用(var sessionnumerator=sessionManager.getsessionnumerator())
{
foreach(sessionnumerator中的var会话)
{
//每个会话将分配给任何进程
//实现您的逻辑以选择首选项
仅将(var meterInformation=session.QueryInterface)用作实验

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
        }
    }
}