Capturing Video Using Camera And Saving Video In Windows 10 Universal App

Let’s see the steps.

Firstly, we need to enable the following capability’s “Video Library” and “Webcam” in the Package.appxmanifest file like the following image.

Video Library

Next we need to add a button to capture Video and Media element control to show the captured video. Then we will save the video in the location of the current system.

Now go to MainPage.xaml and design your UI according to your wish. Here I created application bar with two buttons: one is for capture video and another one is for save video. Create one media element control to view the recorded video.

XAML Code

  1. <Page.BottomAppBar>  
  2.     <AppBarIsOpenAppBarIsOpen="True" IsSticky="True">  
  3.         <StackPanel Orientation="Horizontal">  
  4.             <AppBarButton x:Name="captureBtn" Label="Capture" Icon="Camera" Click="captureBtn_Click"></AppBarButton>  
  5.             <AppBarButton x:Name="saveBtn" Label="Save" Icon="Save" Click="saveBtn_Click"></AppBarButton>  
  6.         </StackPanel>  
  7.         </AppBar>  
  8. </Page.BottomAppBar>  
  9. <Grid Background="{ThemeResourceApplicationPageBackgroundThemeBrush}">  
  10.     <MediaElement x:Name="demoVideo"></MediaElement>  
  11. </Grid>  
Now go to code behind page and add the following code on capture button click event to record the video using camera.
  1. privateStorageFilestoreFile;  
  2. privateIRandomAccessStream stream;  
  3. privateasyncvoidcaptureBtn_Click(object sender, RoutedEventArgs e)  
  4. {  
  5.     CameraCaptureUI video = newCameraCaptureUI();  
  6.     video.VideoSettings.Format = CameraCaptureUIVideoFormat.Mp4;  
  7.     video.VideoSettings.MaxResolution = CameraCaptureUIMaxVideoResolution.HighDefinition;  
  8.     storeFile = awaitvideo.CaptureFileAsync(CameraCaptureUIMode.Video);  
  9.     if (storeFile != null)  
  10.     {  
  11.         stream = awaitstoreFile.OpenAsync(FileAccessMode.Read);  
  12.         MediaClip media = awaitMediaClip.CreateFromFileAsync(storeFile);  
  13.         MediaCompositionmComposition = newMediaComposition();  
  14.         mComposition.Clips.Add(media);  
  15.         MediaStreamSource source = mComposition.GeneratePreviewMediaStreamSource((int) demoVideo.ActualWidth, (int) demoVideo.ActualHeight);  
  16.         demoVideo.SetMediaStreamSource(source);  
  17.     }  
  18. }  
Next go to save button click event and write the following code to save the recorded video.
  1. private async void saveBtn_Click(object sender, RoutedEventArgs e)  
  2. {  
  3.     FileSavePicker save = new FileSavePicker();  
  4.     save.FileTypeChoices.Add("Video", newList < string > ()  
  5.     {  
  6.         ".mp4",  
  7.         ".wmv"  
  8.     });  
  9.     save.DefaultFileExtension = ".mp4";  
  10.     save.SuggestedFileName = "Video";  
  11.     save.SuggestedStartLocation = PickerLocationId.VideosLibrary;  
  12.     save.SuggestedSaveFile = storeFile;  
  13.     var s = awaitsave.PickSaveFileAsync();  
  14.     using(var reader = newDataReader(stream.GetInputStreamAt(0)))  
  15.     {  
  16.         awaitreader.LoadAsync((uint) stream.Size);  
  17.         byte[] buffer = newbyte[(int) stream.Size];  
  18.         reader.ReadBytes(buffer);  
  19.         awaitFileIO.WriteBytesAsync(s, buffer);  
  20.     }  
  21. }  
Now run the app and show the output as in the following screenshot:

Output