Showing posts with label Video Player App. Show all posts

Introduction Hey! Ever wanted to create cool apps for different devices like phones and computers? Well, .NET MAUI makes it super...

.NET MAUI - Video Player using Community Toolkit .NET MAUI - Video Player using Community Toolkit

A blog about android developement

Video Player App

Introduction

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:

  • 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:

  • Open Terminal or Command Prompt: Open a terminal or command prompt window. You can usually find this on your computer by searching for "Command Prompt" (Windows) or "Terminal" (macOS/Linux).
  • Navigate to Your Project Folder: Use the cd command to navigate to your .NET MAUI project folder. For example:
  • Install Community Toolkit: Run the following command to install the CommunityToolkit.Maui package:
    dotnet add package Microsoft.Maui.CommunityToolkit
  • Restore Packages: After the installation, run the following command to restore the packages:
    dotnet restore

Implementation

  • 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 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 Code behind and add the following which will be useful to controls 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.

Introduction In this tutorial, we will learn how to play a video or audio file in Xamarin.Forms applicati...

Video Player App in Xamarin.Forms Video Player App in Xamarin.Forms

A blog about android developement

Video Player App

Video Player App in Xamarin.Forms

Introduction

In this tutorial, we will learn how to play a video or audio file in Xamarin.Forms application. This feature is not available by default in Xamarin Forms. So, we need to add an additional plugin to achieve these features. "MediaManager Plugin" offers this functionality."MediaManager Plugin" offers this functionality.


Media Manager

  • Designed to be simple and easy to use
  • Native playback of media files from remote http(s), embedded and local sources
  • Native media notifications and remote controls
  • Queue and playback management by default
  • Playback status (Playing, Buffering, Loading, Paused, Progress)
  • Events for media handling to hook into

Coding Part

Steps

  1. Step 1: Creating new Xamarin.Forms Projects
  2. Step 2: Setting up the Media Manager in Xamarin.Forms .Net Standard Project
  3. Step 3: Implementation of Xamarin Forms - Video Player App

Step 1: Creating new Xamarin.Forms Projects

Create New Project by selecting "New Project" à "Xamarin Cross Platform App" and Clicking "OK".
Note: Xamarin.Forms version should be greater than 5.0.

Then Select Android and iOS Platforms as shown below with Code Sharing Strategy as PCL or .Net Standard and Click OK.

Step 2: Setting up the Media Manager in Xamarin.Forms .Net Standard Project

    In this step, we will see how to setup the plugin.

  • Open the Nuget Manager in the Visual Studio Solution by right click the solution and selecting “Manage Nuget Packages”.
  • Then select below mentioned 2 plugins and check all the projects in the solution, install the plugin.
    • Plugin.MediaManager
    • Plugin.MediaManager.Forms

Step 3: Implementation of Xamarin Forms - Video Player App

In this step, add a VideoView control to your project. For that, go to Solution Explorer >> double click on MainPage.Xaml. After opening this, you can add VideoView assembly and XAML code to your project.

  • Include the video view control in your xaml file and add the button to perform play/pause operations.
  • We are going to play the video from the url as like in the below snippet.
  • Then add click event to perform the play and pause operations. We need to use the below mentioned code part to peform the play/pause operations using Media Manager
  • The final code behind will look like below.
  • Make sure to call " CrossMediaManager.Init(this, savedInstanceState); " for Android or " CrossMediaManager.Init(this); " for iOS from your platform code before starting playback, otherwise the video View will not be prepared to display the video.

Result

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.