Play Any Media File In Windows 10 Universal Windows App

In Windows 10 we have default control media elements to perform media-related activities.

In Windows 10, Media Control has the capability of playing media files of any kind,  such as a music file of .mp3 or .mp4g format, a picture of .jpg or .png format as well as a video file of .wmv or .avi format, etc. We can add files, play them, pause them, and stop them.

Let’s see the steps

Create a new Windows 10 Universal app.

Design the app using the following XAML code.

  1. <Grid Background="HotPink">  
  2.    <MediaElement x:Name="demoMedia"AreTransportControlsEnabled="True"></MediaElement>  
  3. </Grid>  
AreTransportControlsEnabled used to set a value that determines whether the standard transport controls are enabled, the standard control like play and pause controls for media control.

Now open the media file using the following code:
  1. FileOpenPickeropenMediaFile = new FileOpenPicker();  
  2. openMediaFile.ViewMode = PickerViewMode.Thumbnail;  
  3. openMediaFile.SuggestedStartLocation = PickerLocationId.VideosLibrary;  
  4. openMediaFile.FileTypeFilter.Add(".mp3");  
  5. openMediaFile.FileTypeFilter.Add(".mkv");  
  6. openMediaFile.FileTypeFilter.Add(".mp4");  
  7. StorageFile file = awaitopenMediaFile.PickSingleFileAsync();  
Now play the selected media file using the following code.
  1. demoMedia.AutoPlay = true;  
  2. demoMedia.SetPlaybackSource(MediaSource.CreateFromStorageFile(file));  
  3. demoMedia.Play();  
Full source code looks like the following code.
  1. public async void PlayFile()  
  2. {  
  3.     FileOpenPickeropenMediaFile = new FileOpenPicker();  
  4.     openMediaFile.ViewMode = PickerViewMode.Thumbnail;  
  5.     openMediaFile.SuggestedStartLocation = PickerLocationId.VideosLibrary;  
  6.     openMediaFile.FileTypeFilter.Add(".mp3");  
  7.     openMediaFile.FileTypeFilter.Add(".mkv");  
  8.     openMediaFile.FileTypeFilter.Add(".mp4");  
  9.     StorageFile file = awaitopenMediaFile.PickSingleFileAsync();  
  10.     demoMedia.AutoPlay = true;  
  11.     demoMedia.SetPlaybackSource(MediaSource.CreateFromStorageFile(file));  
  12.     demoMedia.Play();  
  13. }  
Now run the app and see the output looks like the following image.

run

For Source Code.