question

Rashika-2676 avatar image
2 Votes"
Rashika-2676 asked ·

Playing in-built webcam feed in a UWP app stopped working after?

I'm trying to play the built-in webcam feed in a MediaElement within a UWP app. It works fine for a few users but there is no feed played for most and I'm lost on what could be the issue.



Some observations when the webcam feed doesn't play:

  • The code executes without any exceptions

  • The dialog that requests user permission to access the camera is shown

  • The LED indicating the webcam is in use turns on soon as it is executed, but there is no feed.

  • Skype and Camera apps work fine.

The app was working as expected until a week back. A few things that changed in the mean time that could have had an impact are

  • Installed Kaspersky

  • A bunch of windows updates

  • Uninstalled VS2017 professional edition & VS2019 Community edition and installed VS2019 Professional Edition



Some additional information that might be needed to narrow down the reason.

  • Webcam is enabled in the Package manifest of the app

  • App Target version: 18362

  • App Min version: 18362

  • Windows OS Version : 18362

Any help on this would be highly appreciated. Thanks much in advance!

Here is the piece of code used to play the webcam feed where VideoStreamer is a MediaElement.


  private async Task PlayLiveVideo()
         {
             var allGroups = await MediaFrameSourceGroup.FindAllAsync();
             var eligibleGroups = allGroups.Select(g => new
             {
                 Group = g,
    
                 // For each source kind, find the source which offers that kind of media frame,
                 // or null if there is no such source.
                 SourceInfos = new MediaFrameSourceInfo[]
                 {
         g.SourceInfos.FirstOrDefault(info => info.DeviceInformation?.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Front
             && info.SourceKind == MediaFrameSourceKind.Color),
         g.SourceInfos.FirstOrDefault(info => info.DeviceInformation?.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Back
             && info.SourceKind == MediaFrameSourceKind.Color)
                 }
             }).Where(g => g.SourceInfos.Any(info => info != null)).ToList();
    
             if (eligibleGroups.Count == 0)
             {
                 System.Diagnostics.Debug.WriteLine("No source group with front and back-facing camera found.");
                 return;
             }
    
             var selectedGroupIndex = 0; // Select the first eligible group
             MediaFrameSourceGroup selectedGroup = eligibleGroups[selectedGroupIndex].Group;
             MediaFrameSourceInfo frontSourceInfo = selectedGroup.SourceInfos[0];
    
             MediaCapture mediaCapture = new MediaCapture();    
             MediaCaptureInitializationSettings settings = new MediaCaptureInitializationSettings()
             {
                 SourceGroup = selectedGroup,
                 SharingMode = MediaCaptureSharingMode.ExclusiveControl,
                 MemoryPreference = MediaCaptureMemoryPreference.Cpu,
                 StreamingCaptureMode = StreamingCaptureMode.Video, 
             };
             try
             {
                 await mediaCapture.InitializeAsync(settings);
             }
             catch (Exception ex)
             {
                 System.Diagnostics.Debug.WriteLine("MediaCapture initialization failed: " + ex.Message);
                 return;
             } 
             var frameMediaSource1 = MediaSource.CreateFromMediaFrameSource(mediaCapture.FrameSources[frontSourceInfo.Id]);               
             VideoStreamer.SetPlaybackSource(frameMediaSource1);
             VideoStreamer.Play();
         }



windows-uwp
· 9
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Do you mean the MediaElement can't show anything for most users? I tested your code and it worked well, I can't reproduce this issue. If you use MediaPlayerElement to replace MediaElement, can it work?

1 Vote 1 ·

Hi FayWang,

Thanks for the quick response. Yes, I did mean that MediaElement doesn't show anything for most users. The weird part is that for some other users it was working fine( showing the webcam feed), then one day, out of nowhere, it just stopped working (Media Element is blank). I'd be really grateful on any direction on what caused the issue.

I tried using MediaPlayerElement but that didn't help either. Here is the code with VideoStreamer being a MediaPlayerElement. Also VideoFrameAvailable event of the mediaPlayer is never raised.

  var mediaPlayer = new Windows.Media.Playback.MediaPlayer();
             mediaPlayer.Source = frameMediaSource1;
             mediaPlayer.VideoFrameAvailable += MediaPlayer_VideoFrameAvailable;
             VideoStreamer.SetMediaPlayer(mediaPlayer) ;          
             VideoStreamer.AutoPlay = true;
             VideoStreamer.MediaPlayer.Play();
0 Votes 0 ·

Or you could try to use CaptureElement Class, it is also used in conjunction with the MediaCapture API. The MediaCapture captures frame and then pass it as the Source of CaptureElement to show. Here is an official sample you can refer to it and check if it can work.


0 Votes 0 ·
Show more comments

1 Answer

FayWang-MSFT avatar image
2 Votes"
FayWang-MSFT answered ·

Hello,

Welcome to Microsoft Q&A!

The problem is that you installed Kaspersky, the setting of Kaspersky Endpoint Security's "advanced Threat Protection/host intrusion prevention" will prevent all apps outside from our dev-harddrive to access the camera. You need to reconfigure that feature in Kaspersky Endpoint Security(e.g. declare necessary locations/clients as a trusted zone).

·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.