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.
- <Grid Background="HotPink">
- <MediaElement x:Name="demoMedia"AreTransportControlsEnabled="True"></MediaElement>
- </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:
- FileOpenPickeropenMediaFile = new FileOpenPicker();
- openMediaFile.ViewMode = PickerViewMode.Thumbnail;
- openMediaFile.SuggestedStartLocation = PickerLocationId.VideosLibrary;
- openMediaFile.FileTypeFilter.Add(".mp3");
- openMediaFile.FileTypeFilter.Add(".mkv");
- openMediaFile.FileTypeFilter.Add(".mp4");
- StorageFile file = awaitopenMediaFile.PickSingleFileAsync();
Now play the selected media file using the following code.
- demoMedia.AutoPlay = true;
- demoMedia.SetPlaybackSource(MediaSource.CreateFromStorageFile(file));
- demoMedia.Play();
Full source code looks like the following code.
- public async void PlayFile()
- {
- FileOpenPickeropenMediaFile = new FileOpenPicker();
- openMediaFile.ViewMode = PickerViewMode.Thumbnail;
- openMediaFile.SuggestedStartLocation = PickerLocationId.VideosLibrary;
- openMediaFile.FileTypeFilter.Add(".mp3");
- openMediaFile.FileTypeFilter.Add(".mkv");
- openMediaFile.FileTypeFilter.Add(".mp4");
- StorageFile file = awaitopenMediaFile.PickSingleFileAsync();
- demoMedia.AutoPlay = true;
- demoMedia.SetPlaybackSource(MediaSource.CreateFromStorageFile(file));
- demoMedia.Play();
- }
Now run the app and see the output looks like the following image.
For Source
Code.