Every since I did an update on my Windows machine, I can no longer use the camera in my UWP application that uses the MediaCapture
class.
Origninally, I was getting an UnuthorizedAccessException
when trying to call the InitializeAsync()
function but, I managed to fix this by adding the EnableFrameServerMode to my register (via this link: https://www.macecraft.com/fix-webcam-issues-windows-10-anniversary-update/)
However, since doing this, the MediaFrameSource.SupportedFormats
property is empty (MediaFrameSource.SupportedFormats.Count
is 0).
This is only occurring on my machine, colleagues with identical machines (same hardware and software), and different machines, are not experiencing this issue.
I'm currently running:
- Windows 10 Home edition
- Version 10.0.17134 Build 17134
I'm building the app with Xamarin.Forms using Visual Studio version 15.9.4. The Target Version for the app is Windows 10.0 Build 16299 (Min Version is the same).
Here is my code from my ViewRenderer:
var frameSourceGroups = await MediaFrameSourceGroup.FindAllAsync();
MediaFrameSourceGroup selectedGroup = null;
MediaFrameSourceInfo colorSourceInfo = null;
foreach (var sourceGroup in frameSourceGroups)
{
foreach (var sourceInfo in sourceGroup.SourceInfos)
{
if (sourceInfo.MediaStreamType == MediaStreamType.VideoRecord
&& sourceInfo.SourceKind == MediaFrameSourceKind.Color
&& sourceInfo.DeviceInformation.EnclosureLocation != null
&& sourceInfo.DeviceInformation.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Front)
{
colorSourceInfo = sourceInfo;
break;
}
colorSourceInfo = sourceInfo;
}
if (colorSourceInfo != null)
{
selectedGroup = sourceGroup;
break;
}
selectedGroup = sourceGroup;
}
if (selectedGroup == null || colorSourceInfo == null) return;
_mediaCapture = new MediaCapture();
var settings = new MediaCaptureInitializationSettings()
{
SourceGroup = selectedGroup,
SharingMode = MediaCaptureSharingMode.ExclusiveControl,
MemoryPreference = MediaCaptureMemoryPreference.Cpu,
StreamingCaptureMode = StreamingCaptureMode.Video
};
try
{
await _mediaCapture.InitializeAsync(settings);
}
catch (UnauthorizedAccessException)
{
Debug.WriteLine("The app was denied access to the camera");
return;
}
catch (Exception ex)
{
Debug.WriteLine("MediaCapture initialization failed: " + ex.Message);
return;
}
If the EnableFrameServerMode is not set to 0 in my registry, I get an UnauthorizedAccessException
. If it is:
MediaFrameSource colorFrameSource = null;
foreach (var keyValuePairFrameSource in _mediaCapture.FrameSources)
{
if (keyValuePairFrameSource.Key.ToLowerInvariant().Contains(colorSourceInfo.SourceGroup.Id.ToLowerInvariant()))
{
colorFrameSource = keyValuePairFrameSource.Value;
}
}
_config = ConfigurationHelper.GetConfigData();
var preferredFormat = colorFrameSource?.SupportedFormats.FirstOrDefault(format =>
format.VideoFormat.Width == _config.CameraPreviewWidth &&
format.VideoFormat.Height == _config.CameraPreviewHeight &&
format.Subtype == MediaEncodingSubtypes.Nv12.ToUpper());
if (preferredFormat == null)
{
Debug.WriteLine("Our desired format is not supported");
return;
}
SupportedFormats
List is empty, causing our preferredFormat
object to be null.
If I don't return when preferredFormat
is null, and do this:
var _mediaFrameReader = await _mediaCapture.CreateFrameReaderAsync(colorFrameSource, MediaEncodingSubtypes.Nv12);
_mediaFrameReader.FrameArrived += ColorFrameReader_FrameArrived;
await _mediaFrameReader.StartAsync();
I just get a screen with a black background where the camera frames should be.