private async Task StartRecordingAsync(RecordingOptions options) { // Encoders generally like even numbers var width = EnsureEven(options.Resolution.Width); var height = EnsureEven(options.Resolution.Height); // Find a place to put our vidoe for now var file = await GetTempFileAsync(); // Kick off the encoding try { using (var stream = await file.OpenAsync(FileAccessMode.ReadWrite)) using (_encoder = new Encoder(_device, options.Target)) { var surface = _encoder.CreatePreviewSurface(Window.Current.Compositor); _previewBrush.Surface = surface; await _encoder.EncodeAsync( stream, width, height, options.Bitrate, options.FrameRate); } } catch (Exception ex) { Debug.WriteLine(ex.Message); Debug.WriteLine(ex); var message = GetMessageForHResult(ex.HResult); if (message == null) { message = $"Uh-oh! Something went wrong!\n0x{ex.HResult:X8} - {ex.Message}"; } var dialog = new MessageDialog( message, "Recording failed"); await dialog.ShowAsync(); // Go back to the main page Frame.GoBack(); return; } // At this point the encoding has finished, let the user preview the file Frame.Navigate(typeof(SavePage), file); }
private void StartRecordingButton_Click(object sender, RoutedEventArgs e) { if (_preview == null) { throw new InvalidOperationException("There is no current preview!"); } // Get our encoder properties var frameRateItem = (FrameRateItem)FrameRateComboBox.SelectedItem; var resolutionItem = (ResolutionItem)ResolutionComboBox.SelectedItem; var bitrateItem = (BitrateItem)BitrateComboBox.SelectedItem; var useSourceSize = resolutionItem.IsZero(); var width = resolutionItem.Resolution.Width; var height = resolutionItem.Resolution.Height; var bitrate = bitrateItem.Bitrate; var frameRate = frameRateItem.FrameRate; // Use the capture item's size for the encoding if desired if (useSourceSize) { var targetSize = _preview.Target.Size; width = (uint)targetSize.Width; height = (uint)targetSize.Height; } var resolution = new SizeUInt32() { Width = width, Height = height }; var recordingOptions = new RecordingOptions(_preview.Target, resolution, bitrate, frameRate); _preview.Dispose(); _preview = null; StartRecordingButton.IsEnabled = false; Frame.Navigate(typeof(RecordingPage), recordingOptions); }