英文:
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 && 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#)?
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论