如何使用C#检测麦克风是否正在使用

huangapple go评论97阅读模式
英文:

How To Detect That Microphone Is In The Use Using C#

问题

我有一个UWP应用程序,我正在尝试检测麦克风是否被任何应用程序使用。

以下是从我的系统获取麦克风的代码:

namespace CallDetector
{
    public sealed partial class MainPage : Page
    {
        private DispatcherTimer timer;
        private MediaCapture mediaCapture;
       
        public MainPage()
        {
            this.InitializeComponent();
            StartMicrophoneStatusCheckTimer();
        }

        private void StartMicrophoneStatusCheckTimer()
        {
            timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromSeconds(5);
            timer.Tick += Timer_Tick;
            timer.Start();
        }

        private async void Timer_Tick(object sender, object e)
        {
            await CheckMicrophoneStatus();
        }

        private async Task CheckMicrophoneStatus()
        {
            var microphoneDevice = await GetMicrophoneDeviceAsync();

            if (microphoneDevice != null && microphoneDevice.IsEnabled)
            {
                var msg = new MessageDialog($"发现麦克风设备:{microphoneDevice.Name}");
                await msg.ShowAsync();
            }
            else
            {
                var msge = new MessageDialog("未找到麦克风设备。");
                await msge.ShowAsync();
            }
        }

        private static async Task<DeviceInformation> GetMicrophoneDeviceAsync()
        {
            var microphoneSelector = MediaDevice.GetAudioCaptureSelector();
            var microphoneDevices = await DeviceInformation.FindAllAsync(microphoneSelector);
            return microphoneDevices.FirstOrDefault();
        }

    }
}

我如何检测麦克风正在被哪个应用程序使用(使用UWP C#)?

英文:

I have one UWP application in which I am trying to detect if microphone is used by any application or not.

Here is the code to get the microphone from my system.

namespace CallDetector
{
    public sealed partial class MainPage : Page
    {
        private DispatcherTimer timer;
        private MediaCapture mediaCapture;
       
        public MainPage()
        {
            this.InitializeComponent();
            StartMicrophoneStatusCheckTimer();
        }

        private void StartMicrophoneStatusCheckTimer()
        {
            timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromSeconds(5);
            timer.Tick += Timer_Tick;
            timer.Start();
        }

        private async void Timer_Tick(object sender, object e)
        {
            await CheckMicrophoneStatus();
        }

        private async Task CheckMicrophoneStatus()
        {
            var microphoneDevice = await GetMicrophoneDeviceAsync();

            if (microphoneDevice != null &amp;&amp; microphoneDevice.IsEnabled)
            {
                var msg = new MessageDialog($&quot;Microphone device found: {microphoneDevice.Name}&quot;);
                await msg.ShowAsync();
            }
            else
            {
                var msge = new MessageDialog(&quot;No microphone device found.&quot;);
                await msge.ShowAsync();
            }
        }

        private static async Task&lt;DeviceInformation&gt; GetMicrophoneDeviceAsync()
        {
            var microphoneSelector = MediaDevice.GetAudioCaptureSelector();
            var microphoneDevices = await DeviceInformation.FindAllAsync(microphoneSelector);
            return microphoneDevices.FirstOrDefault();
        }

    }
}

How can I detect that microphone is used by which application (By using the UWP C#)?

答案1

得分: 1

我在Namespace Windows.Media.Devices中检查了API,目前没有一个可以获取哪个应用程序正在使用麦克风的API。为了确保,我还检查了Win32 音频会话事件,也无法获取正在使用麦克风的进程,它只能注册并接收一些特定会话事件的通知。

英文:

I checked the API in Namespace Windows.Media.Devices, currently there is no API that can get which app is using the microphone. Just in case, I also checked Win32 Audio Session Events, also couldn't get which process is using the microphone, it can only register and receive notifications of some specific session events.

huangapple
  • 本文由 发表于 2023年6月15日 19:27:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76481991.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定