How To Use Media Element Control To Open A Media File In Universal Application Development With XAML And C#

Before reading this article, please go through the article, given below-

  1. Introduction To Universal Windows Platform (UWP) App Development Using Windows 10 And Visual Studio 2015

Reading this article, you can learn, how to use Media Element Control with Media Transport Control to open a Media File in Universal Windows Apps development with XAML and Visual C#.

The following important tools are required for developing UWP-

  1. Windows 10 (Recommended)
  2. Visual Studio 2015 Community Edition (It is a free software available online)

Now, we can discuss step by step App development.

Step 1: Open Visual Studio 2015 -> Start -> New Project-> Select Universal (under Visual C#->Windows)-> Blank App -> Give the suitable name for your App (UWPMediaElementMTP) ->OK.

Blank App

Step 2: Choose Target and minimum platform version for your Windows Universal Application. After the project, create App.xaml and MainPage.xaml.

version

Step 3: Open (double Click) the file MainPage.xaml in the Solution Explorer and click on the Toolbox tab on the left to open the list of Common XAML controls. Expand Common XAML Controls and drag the MediaElement Control to the middle of the design canvas and Change the name property.

Enable the checkbox AreTransportControlsEnabled property. 

property

Note: Automatically, the code, given below, will be generated in XAML code view, while we are done in the design view-

  1. <Page x:Class="UWPMediaElementMTP.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:UWPMediaElementMTP" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d">  
  2.     <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">  
  3.         <MediaElement x:Name="mediaEletest" AreTransportControlsEnabled="True" /> </Grid>  
  4. </Page>  
Step 4: Add the Namespaces, given below, in MainPage.xaml.cs-
  1. using Windows.Storage.Pickers;  
  2. using Windows.Storage;  
  3. using Windows.Media.Core;  
These name spaces are used for FilePicker, Media and Storage.

Namespaces

Step 5: Create a new async method called OpenMedia() in MainPage.xaml.cs and add the code, given below, used to open the file, using File Picker and play the selected media file.
  1. public async void OpenMedia()  
  2. {  
  3.     //Using File Picker top open your Media File  
  4.     FileOpenPicker MediaContent = new FileOpenPicker();  
  5.     MediaContent.ViewMode = PickerViewMode.List;  
  6.     MediaContent.SuggestedStartLocation = PickerLocationId.VideosLibrary;  
  7.     //add the media file ectension for filtering  
  8.     MediaContent.FileTypeFilter.Add(".wmv");  
  9.     MediaContent.FileTypeFilter.Add(".wma");  
  10.     MediaContent.FileTypeFilter.Add(".mp3");  
  11.     MediaContent.FileTypeFilter.Add(".mp4");  
  12.     StorageFile openmedia = await MediaContent.PickSingleFileAsync();  
  13.     mediaEletest.AutoPlay = true;  
  14.     mediaEletest.SetPlaybackSource(MediaSource.CreateFromStorageFile(openmedia));  
  15.     mediaEletest.Play();  
  16. }  
Step 6: Call the OpenMedia() Method in constructor method (MainPage() method) in MainPage.xaml.cs.

Now, the constructor method looks like-
  1. public MainPage()   
  2. {  
  3.     this.InitializeComponent();  
  4.     OpenMedia();  
  5. }  
constructor method

Step 7: Deploy your app in the local machine and the output of the UWPMediaElementMTP app is-

To pick the media file, the screenshot is given below- 

Media file

To play the media file, the screenshot is given below- 

Playing the Media File

Summary: Now, you successfully created and tested your media element control in Visual C# - UWP environment.

 

Up Next
    Ebook Download
    View all
    Learn
    View all