Video capturing is one of the common tool used now a days in application development. It is quite obvious a person might be needing to integrate Video capturing in his Windows 10 Application. So, here we are beginning with our Video Capturing application:
In Visual Studio 2015, Start a new project and select Blank app in Universal App category. Before starting any coding, we have to go to package.appxmanifest file of the solution, we need to select Webcam and Video Library under Capabilities.
Then in the MainPage.xaml file we will add the code for button which will be used to capture the video and a media element where video will be shown:
- <Button x:Name="Capture_button" Content="Start Capturing Video" HorizontalAlignment="Left" Margin="79,240,0,0" VerticalAlignment="Top" Height="78" Width="169" Click="Capture_button_Click"/>
- <MediaElement x:Name="video" HorizontalAlignment="Left" Height="720" Margin="312,0,0,0" VerticalAlignment="Top" Width="968" AreTransportControlsEnabled="True"/>
- <Button x:Name="Save_button" Content="Save Video" HorizontalAlignment="Left" Margin="79,391,0,0" VerticalAlignment="Top" Height="78" Width="169" Click="save_Video"/>
After adding the above code in the xaml file, we will go to the methods of the buttons: Capture_button and Save_button and add their functionality. But before adding the functionality to the methods, we need to add the namespaces which will be required in the application:
- using Windows.Media.Capture;
- using Windows.Media.Core;
- using Windows.Media.Editing;
- using Windows.Storage;
- using Windows.Storage.Pickers;
- using Windows.Storage.Streams;
- using Windows.UI.Popups;
- using Windows.UI.Xaml;
- using Windows.UI.Xaml.Controls;
Now, we add the declaration code for two objects of StorageFile class and IRandomAccessStream class which will be used throughout the application:
- private StorageFile sf;
- private IRandomAccessStream rs;
Now, the functionality for Capture_button method which will invoke the default video capturing tool of the system:
- private async void Capture_button_Click(object sender, RoutedEventArgs e)
- {
-
- CameraCaptureUI cc = new CameraCaptureUI();
- cc.VideoSettings.Format = CameraCaptureUIVideoFormat.Mp4;
- cc.VideoSettings.MaxResolution = CameraCaptureUIMaxVideoResolution.HighDefinition;
-
-
-
- sf = await cc.CaptureFileAsync(CameraCaptureUIMode.Video);
- if (sf != null)
- {
- rs = await sf.OpenAsync(FileAccessMode.Read);
- MediaClip mc = await MediaClip.CreateFromFileAsync(sf);
- MediaComposition mcomp = new MediaComposition();
- mcomp.Clips.Add(mc);
- MediaStreamSource mss = mcomp.GeneratePreviewMediaStreamSource((int) video.ActualWidth, (int)video.ActualHeight);
- video.SetMediaStreamSource(mss);
- }
- }
Now, as the save functionality we used in the last blog of Capture and save Image, same we will be using here but with minor adjustments for the Video file extensions:
- private async void save_Video(object sender, RoutedEventArgs e)
- {
-
- try
- {
-
- FileSavePicker fs = new FileSavePicker();
- fs.FileTypeChoices.Add("Video", new List<string>() { ".mp4",".3gp" });
- fs.DefaultFileExtension = ".mp4";
- fs.SuggestedFileName = "Video" + DateTime.Today.ToString();
- fs.SuggestedStartLocation = PickerLocationId.VideosLibrary;
-
-
- fs.SuggestedSaveFile = sf;
-
-
- var s = await fs.PickSaveFileAsync();
- if (s != null)
- {
- using (var dataReader = new DataReader(rs.GetInputStreamAt(0)))
- {
- await dataReader.LoadAsync((uint)rs.Size);
- byte[] buffer = new byte[(int)rs.Size];
- dataReader.ReadBytes(buffer);
- await FileIO.WriteBytesAsync(s, buffer);
- }
- }
- }
- catch (Exception ex)
- {
- var messageDialog = new MessageDialog("Something went wrong.");
- await messageDialog.ShowAsync();
- }
- }
Now, run the program and enjoy!