Opening And Playing A Video File In Windows 10

Here we are using a FilePicker to open a file and a MediaElement to play the same.

Step 1: Open a blank app and add a MediaElement and a TextBlock either from the toolbox or by copying the following XAML code into your grid:

  1. <TextBlock Text="Video" FontSize="20"></TextBlock>  
  2. <StackPanel Margin="0,40,0,0">  
  3.     <Button Name="open" Content="Open" Margin="15,0,0,0" Width="120" Height="30" Click="open_Click"></Button>  
  4.     <MediaElement x:Name="myMedia" AutoPlay="False" Margin="5" HorizontalAlignment="Stretch" AreTransportControlsEnabled="True"></MediaElement>  
  5. </StackPanel>  

  

Step 2: Add the following namespaces to your project, which is needed in further C# code.

  1. using Windows.Media.Core;  
  2. using Windows.Storage;  
  3. using Windows.Storage.Pickers;  

Step 3: Copy and paste the following code to the cs pag, which will be called on button click event.

  1. private async void open_Click(object sender, RoutedEventArgs e)   
  2. {  
  3.     FileOpenPicker openPicker = new FileOpenPicker();  
  4.     openPicker.ViewMode = PickerViewMode.Thumbnail;  
  5.     openPicker.SuggestedStartLocation = PickerLocationId.VideosLibrary;  
  6.     openPicker.FileTypeFilter.Add(".mp4");  
  7.     openPicker.FileTypeFilter.Add(".mkv");  
  8.     openPicker.FileTypeFilter.Add(".avi");  
  9.   
  10.     StorageFile file = await openPicker.PickSingleFileAsync();  
  11.   
  12.     myMedia.AutoPlay = true;  
  13.     myMedia.SetPlaybackSource(MediaSource.CreateFromStorageFile(file));  
  14.     myMedia.Play();  
  15. }  

Step 4: Run your application and test yourself.

 

Up Next
    Ebook Download
    View all
    Learn
    View all