Search code examples
.netxamarinc#-4.0uwpuwp-xaml

How To Detect That Microphone Is In The Use Using 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 && microphoneDevice.IsEnabled)
            {
                var msg = new MessageDialog($"Microphone device found: {microphoneDevice.Name}");
                await msg.ShowAsync();
            }
            else
            {
                var msge = new MessageDialog("No microphone device found.");
                await msge.ShowAsync();
            }
        }

        private static async Task<DeviceInformation> 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#)?


Solution

  • 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.