private async Task StartVideoDeviceAsync() { try { if (mediaCapture == null || mediaCapture.CameraStreamState == CameraStreamState.Shutdown || mediaCapture.CameraStreamState == CameraStreamState.NotStreaming) { mediaCapture?.Dispose(); var selectedCamera = await CameraUtilities.FindBestCameraAsync(DeviceClass.VideoCapture); if (selectedCamera == null) { await new MessageDialog("There are no cameras connected, please connect a camera and try again.").ShowAsync(); mediaCapture?.Dispose(); mediaCapture = null; return; } mediaCapture = new MediaCapture(); // put reference in App so that it can be disposed if app is suspended App.MediaCaptureManager = mediaCapture; await mediaCapture.InitializeAsync(new MediaCaptureInitializationSettings { VideoDeviceId = selectedCamera.Id }); WebCamCaptureElement.Source = mediaCapture; } if (mediaCapture.CameraStreamState == CameraStreamState.NotStreaming) { // Frame lock timer logic credit: Rene Shulte, see https://github.com/sevans4067/WinMl-TinyYOLO/blob/master/TinyYOLO/MainPage.xaml.cs if (frameProcessingTimer != null) { frameProcessingTimer.Cancel(); frameProcessingSemaphore.Release(); } frameProcessingTimer = ThreadPoolTimer.CreatePeriodicTimer(ProcessCurrentVideoFrame, TimeSpan.FromMilliseconds(66)); // 1000 (ms) / 15 (fps) == 66 (ms) videoProperties = mediaCapture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview) as VideoEncodingProperties; await mediaCapture.StartPreviewAsync(); App.GlobalDisplayRequest.RequestActive(); WebCamCaptureElement.Visibility = Visibility.Visible; } } catch (Exception ex) { await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => StatusBlock.Text = $"StartVideoDeviceAsync Error: {ex.Message}"); } }
private async Task InitializeVideoAsync() { ReloadVideoStreamButton.Visibility = Visibility.Collapsed; ShowBusyIndicator("Initializing..."); try { currentState = RecordingState.NotInitialized; PreviewMediaElement.Source = null; ShowBusyIndicator("starting video device..."); mediaCapture = new MediaCapture(); // put reference in App so that it can be disposed if app is suspended App.MediaCaptureManager = mediaCapture; selectedCamera = await CameraUtilities.FindBestCameraAsync(); if (selectedCamera == null) { await new MessageDialog("There are no cameras connected, please connect a camera and try again.").ShowAsync(); await DisposeMediaCaptureAsync(); HideBusyIndicator(); return; } await mediaCapture.InitializeAsync(new MediaCaptureInitializationSettings { VideoDeviceId = selectedCamera.Id }); if (mediaCapture.MediaCaptureSettings.VideoDeviceId != "" && mediaCapture.MediaCaptureSettings.AudioDeviceId != "") { ShowBusyIndicator("camera initialized.."); mediaCapture.Failed += Failed; } else { ShowBusyIndicator("camera error!"); } //------starting preview----------// ShowBusyIndicator("starting preview..."); PreviewMediaElement.Source = mediaCapture; await mediaCapture.StartPreviewAsync(); currentState = RecordingState.Previewing; } catch (UnauthorizedAccessException ex) { Debug.WriteLine($"InitializeVideo UnauthorizedAccessException\r\n {ex}"); ShowBusyIndicator("Unauthorized Access Error"); await new MessageDialog("-----Unauthorized Access Error!-----\r\n\n" + "This can happen for a couple reasons:\r\n" + "-You have disabled Camera access to the app\r\n" + "-You have disabled Microphone access to the app\r\n\n" + "To fix this, go to Settings > Privacy > Camera (or Microphone) and reenable it.").ShowAsync(); await DisposeMediaCaptureAsync(); } catch (Exception ex) { ShowBusyIndicator("Initialize Video Error"); await new MessageDialog("InitializeVideoAsync() Exception\r\n\nError Message: " + ex.Message).ShowAsync(); currentState = RecordingState.NotInitialized; PreviewMediaElement.Source = null; } finally { HideBusyIndicator(); } }