In this article, we are going to play a video and an audio in Xamarin.Forms application.There is no default function available for playing the video or audio. So, we need to add a plugin for this.
Xamarin.Forms = search "Plugin.MediaManager.Forms".
Xamarin.Android = search "Plugin.MediaManager".
MediaManager Cross-platform media plugin features
- Designed to be simple and easy to play
- Stand alone for easy integration with existing projects and frameworks
- Native playback of media files from remote and local sources
- Native media notifications and remote controls
- Playback status(Playing, Buffering, Loading, Paused, Progress)
You can create Xamarin.Forms app by going to File >> New >> Visual C# >>Cross platform >> Cross-platform App (Xamarin.Native or Xamarin.Forms), give the application name, and press OK.
(Project Name : VideoPlayerApp)
After the project creation, add the following NuGet Packages to your project.
For that, go to Solution Explorer and select your solution. Right-click and select Manage NuGet Packages for the Solution". Now, select the following NuGet Package and select your project to install it.
In this step, add a VideoView control to your project. For that, go to Solution Explorer \>\> VideoPlayerApp (PCL) \>\> double click on MainPage.Xaml. After opening this, you can add VideoView assembly and XAML code to your project.
Write the code as given below.
Next, open Solution Explorer >>VideoPlayerApp(PCL) >> MainPage.xaml.cs page and double-click to open its design view. The code is given below.
Make sure to call "VideoViewRenderer.Init(); " from your platform code before starting playback, otherwise the video View will not be prepared to display the video.
Add Android project by going to Solution Explorer >> VideoPlayerApp.Droid >>MainActivity.cs and double-click to open its design View. Here is the code.
** ** Add iOS project. For that, go to Solution Explorer >> VideoPlayerApp.IOS >>AppDelegate.cs and click open AppDelegate.cs. Here is the code for this page.
Universal Windows Project
Add Universal Windows project. For that, open Solution Explorer \>\>VideoPlayerApp.UWP \>\> MainPage.xaml.cs and click open MainPage.xaml.cs.
Add the below given code to this page.
** Click ' F5 ' or "Build " to run your projects. Running this project, you will have the result like below.**
** Finally, we have successfully created a Xamarin.Forms VideoPlayerApp Application.**