How to play YouTube or computer videos using dot NET MAUI MediaElement

 How to play YouTube or computer videos using dot NET MAUI MediaElement. Today our topic is about MediaElement class in .NET MAUI to play videos from different sources.DotNet MAUI is a cross-platform framework to create amazing apps. 

How to play YouTube or computer videos using dot NET MAUI MediaElement

.NET MAUI has MediaElement class which is used to play media from different sources. Now we can play different videos on the internet or on our own computers. It helps developers play media content in a very simple way. 

MediaElement control and properties

Just just remember about web browser Control internet C# where we can play videos with the help of web browser control. Just like this .NET MAUI has its own amazing control which is MediaElement. 

MediaElement is already a part of several community toolkits. In the previous few years Xamarin. Forms developers build their applications with MediaElement.  I am giving special thanks to Peter Foot who is an amazing programmer for building open-source libraries since 2003.

Some of the properties can be customized for media playback. These properties are 

  • Autoplay properties 
  • Stop and Start properties 
  • Automatic start and volume property and many others 

Dot NET MAUI is one of the growing framework and becoming a biggest platform for future development. Every year it gives developers fixes of problems and issues. 

MediaElement has also the problem of not performing for all kinds of Xamarin.Forms supported platforms. Sometimes it also crash due to live video play through any sources live stream from YouTube. 

Video supporting environments for different platforms 

While working with dot NET MAUI there are lots of options to play videos. For Android, we can implement Exoplayer very well because it does not create issues with live-stream videos. On the other hand for an iOS device, we can use AVPlayer and for windows, we can use MediaElement or MediaPlayer. 

Example 

To play with media element you need to follow these steps and take the example of MediaElement where I use different properties and events. 

1) First you have to include media toolkit library which live inside community Toolkit. And don't worry it leaves in a separate package so you don't worry to install or make any dependency. 

Go to NuGet Packages > search for CommunityToolkit.Maui.MediaElement and install it. 

There are few libraries and dependency which are actually used for Android. And we need to get a sport of .Net 7 For making app with Exo Player to play any video. 

2) Second thing we don't have to update .Net 6 to .Net 7 because it has long term support policies for dotnet Maui. 

3) Third step is to include media element package and it should be initialise in main program.cs. After including we will be able to perform anything with media element in xaml form.

So we have to include media element package just like this: 

.UseMauiCommunityToolkitMediaElement() 

And also using CommunityToolKit.Maui in the project

4) 4rth Step is to include necessary classes and toolkits in mainpage.xaml 

include these in top: 

xmlns:toolkit="http://schemas. microsoft.com/dotnet/2022/maui/toolkit" 

x:Class="MauiMediaElemetSample.MainPage" 

5) 5th setp is to code like this 

How to play YouTube or computer videos using dot NET MAUI MediaElement

For MainPage.xaml.cs

How to play YouTube or computer videos using dot NET MAUI MediaElement


Post a Comment

0 Comments