Creating Exciting Apps with .NET MAUI's Video Player and Community Toolkit.
Hey! Ever wanted to create cool apps for different devices like phones and computers? Well, .NET MAUI makes it super easy. It's like a superhero for app creators! Now, imagine adding fun things like videos to your apps. That's where the Community Toolkit comes in – a toolbox that lots of developers work on together. Today, we're exploring the Video Player control, a cool feature powered by .NET MAUI and its friendly Community Toolkit. Get ready for an adventure in making your apps more exciting with videos!
Quick Links
- Project Setup
- Install Plugin
- Implementation
- Demo
- Download Code
Project Setup
- Launch Visual Studio 2022, and in the start window click Create a new project to create a new project.
- In the Create a new project window, select MAUI in the All project types drop-down, select the .NET MAUI App template, and click the Next button.
- In the Configure your new project window, name your project, choose a suitable location for it, and click the Next button.
- In the Additional Information window, click the Create button.
- Once the project is created, we can able to see the Android, iOS, Windows, and other running options in the toolbar. Press the emulator or run button to build and run the app.
Install Plugin
Implementation of Video Player
- First, we need to open "MauiProgram.cs" and include the following namespace and line to allow the app to use the Chart Library.
using CommunityToolkit.Maui;
.UseMauiCommunityToolkit()
.UseMauiCommunityToolkitMediaElement()
- Open the MainPage.xaml file and add the following namespace. (the page will be replaced according to you).
xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
- Then, remove the default content and add an instance of the Media Element class to the page.
<toolkit:MediaElement Source="https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4"
ShouldShowPlaybackControls="True"
BackgroundColor="AliceBlue"
x:Name="mediaElement"/>
- We can add other controls for custom video controls like play/pause and volume controls like below.
<HorizontalStackLayout BindingContext="{x:Reference mediaElement}"
HorizontalOptions="Center"
Spacing="10">
<Button Text="Play"
HorizontalOptions="CenterAndExpand"
Clicked="OnPlayPauseButtonClicked">
</Button>
<Button Text="Stop"
HorizontalOptions="CenterAndExpand"
Clicked="OnStopButtonClicked">
</Button>
</HorizontalStackLayout>
<Slider Maximum="1.0"
Minimum="0.0"
Value="{Binding Volume}"
ValueChanged="Slider_ValueChanged"
Rotation="270"
WidthRequest="100" />
- Open the Code behind and add the following which will be useful to control media elements using custom controls.
void OnPlayPauseButtonClicked(object sender, EventArgs args)
{
if (mediaElement.CurrentState == MediaElementState.Stopped ||
mediaElement.CurrentState == MediaElementState.Paused)
{
mediaElement.Play();
}
else if (mediaElement.CurrentState == MediaElementState.Playing)
{
mediaElement.Pause();
}
}
void OnStopButtonClicked(object sender, EventArgs args)
{
mediaElement.Stop();
}
private void Slider_ValueChanged(object sender, ValueChangedEventArgs e)
{
mediaElement.Volume = e.NewValue;
}
Download Code
You can download the code from GitHub. If you have any doubts, feel free to post a comment. If you liked this article, and it is useful to you, do like, share the article & star the repository on GitHub.