检查WPF上的麦克风隐私设置

时间:2018-08-10 14:09:48

标签: c# wpf windows winapi

我有一个用c#编写的WPF应用程序,该应用程序将麦克风设备与naudio库一起使用,在Windows 10更新版本1803上,该麦克风添加了用于访问麦克风的隐私设置。

如果用户具有允许隐私标记,则我的应用程序可以正常工作,否则我的应用程序将无法工作。那么如何通过c#检查此隐私设置?

2 个答案:

答案 0 :(得分:0)

似乎没有直接的方法可以确定您的应用程序是否具有这些权限,因此,最好的选择是尝试访问麦克风并在发生错误时捕获错误。

try
{
// code to access microphone
}
catch (System.UnauthorizedAccessException e)
{
// notify user application can't work without microphone permission                
}

答案 1 :(得分:0)

据我所知,这是解决问题的一种方法。不幸的是,它可能会捕获其他与“麦克风隐私设置”无关的错误。

/// <summary>
/// With Windows 10 update 1803 came an option to deny access to the microphones on an OS level.
/// The option covers all soundcards installed into the PC (Magnum/Callisto is a soundcard)
/// </summary>
public static class MicrophonePrivacyProbe
{
  /// <summary>
  /// Test if Microphone Privacy Settings are to restrictive for microphone access.
  /// </summary>
  /// <returns>True if microphone is accessible</returns>
  public static bool Allowed()
  {
    bool access = false;
    var devices = new CaptureDevicesCollection();

    if ( devices?.Count <= 0 ) return false;

    var captureDevice = new Capture(devices[0].DriverGuid);
    CaptureBuffer applicationBuffer = null;

    var inputFormat = new WaveFormat();
    inputFormat.AverageBytesPerSecond = 8000;
    inputFormat.BitsPerSample = 8;
    inputFormat.BlockAlign = 1;
    inputFormat.Channels = 1;
    inputFormat.FormatTag = WaveFormatTag.Pcm;
    inputFormat.SamplesPerSecond = 8000;

    CaptureBufferDescription bufferdesc = new CaptureBufferDescription();
    bufferdesc.BufferBytes = 200;
    bufferdesc.Format = inputFormat; 

    try
    {
      applicationBuffer = new CaptureBuffer(bufferdesc, captureDevice);
      access = true;
    }
    catch (SoundException e)
    {
    }
    finally
    {
      applicationBuffer?.Dispose();
      captureDevice?.Dispose();
    }

    return access;
  }
}