Introduction
In this tutorial, we will learn how to play a video or audio file in the Xamarin.Forms application. This feature is not available by default in Xamarin Forms. So, we need an additional plugin to achieve these features. "MediaManager Plugin" offers this functionality.
Media Manager
- Designed for simple and easy 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
- Creating new Xamarin.Forms Projects
- Setting up the Media Manager in Xamarin.Forms .Net Standard Project
- 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 set up the plugin.
- Open the Nuget Manager in the Visual Studio Solution by right click the solution and selecting “Manage Nuget Packages”.
Then select the below mentioned two 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, you will add a VideoView control to your project. For this, go to Solution Explorer and 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 in the below snippet.
- Then add a click event to perform the play and pause operations. We need to use the below-mentioned code part to perform the play/pause operations using Media Manager.
- The final code behind will look like the 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 questions or 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. To know more about Xamarin Media Manager, read its Wiki
here.