简体   繁体   中英

Find the App which is using front camera in Laptop using UWP/C#

I have to hide the camera preview and off a toggle button when already camera is in use by another application. For this I am using _mediaCapture.Failed += MediaCapture_Failed; event to capture the camera status in side InitializeCameraAsync() method. But when I minimize and maximize the UWP App very quickly, the event is raising and getting error like 'Camera is in use' for the same App it self. So is there any way to find which App is using the Camera currently?

So is there any way to find which App is using the Camera currently?

No there is no way to know which app is using the Camera exactly. But in UWP, you could know if the Camera is being used before you start to use the camera.

You can register a handler for the MediaCapture.CaptureDeviceExclusiveControlStatusChanged event , which is raised whenever the exclusive control status of the device changes. Then you could check the MediaCaptureDeviceExclusiveControlStatusChangedEventArgs.Status property to see if the Camera is available now.

Here is the code sample that you could refer to:

private async void _mediaCapture_CaptureDeviceExclusiveControlStatusChanged(MediaCapture sender, MediaCaptureDeviceExclusiveControlStatusChangedEventArgs args)
{
    if (args.Status == MediaCaptureDeviceExclusiveControlStatus.SharedReadOnlyAvailable)
    {
        ShowMessageToUser("The camera preview can't be displayed because another app has exclusive access");
    }
    else if (args.Status == MediaCaptureDeviceExclusiveControlStatus.ExclusiveControlAvailable && !isPreviewing)
    {
        await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
        {
            await StartPreviewAsync();
        });
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM