Introduction

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.
Nuget Packages

Xamarin.Forms = search "Plugin.MediaManager.Forms".
Xamarin.Android = search"Plugin.MediaMager".
MediaMager 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)


Step 1

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)

Step 2

After the project creation, add the following NuGet Packages to your project.
  • Plugin.MediaManager
  • Plugin.MediaManager.Forms

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.

  • Plugin.MediaManager.Forms
Step 3

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.
Assembly
  1. xmlns:forms="clr-namespace:Plugin.MediaManager.Forms;assembly=Plugin.MediaManager.Forms"
XAML code
  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  4. xmlns:local="clr-namespace:VideoPlayerApp"
  5. x:Class="VideoPlayerApp.MainPage"
  6. xmlns:forms="clr-namespace:Plugin.MediaManager.Forms;assembly=Plugin.MediaManager.Forms"
  7. Title="Video Player">
  8. <ContentPage.Content>
  9. <StackLayout>
  10. <Label Text="Xamarin Forms"
  11. FontSize="40"
  12. TextColor="Azure"/>
  13. <Label Text="Video Player Application"
  14. FontSize="58"
  15. TextColor="BlueViolet"/>
  16. <Button x:Name="PlayStopButtonText"
  17. Text="Play"
  18. Clicked="PlayStopButton"
  19. TextColor="BlueViolet"/>
  20. <forms:VideoView HeightRequest="202"
  21. WidthRequest="202"
  22. />
  23. </StackLayout>
  24. </ContentPage.Content>
  25. </ContentPage>
Step 4

Next, open Solution Explorer >> VideoPlayerApp(PCL) >> MainPage.xaml.cs page and double-click to open its design view. The code is given below.
cs code
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Xamarin.Forms;
  7. using Plugin.MediaManager.Forms;
  8. using Plugin.MediaManager;
  9. using Plugin.MediaManager.Abstractions.Enums;
  10. namespace VideoPlayerApp
  11. {
  12. public partial class MainPage : ContentPage
  13. {
  14. private string videoUrl = "https://sec.ch9.ms/ch9/e68c/690eebb1-797a-40ef-a841-c63dded4e68c/Cognitive-Services-Emotion_high.mp4";
  15. public MainPage()
  16. {
  17. InitializeComponent();
  18. }
  19. private void PlayStopButton(object sender,EventArgs e)
  20. {
  21. if (PlayStopButtonText.Text == "Play")
  22. {
  23. CrossMediaManager.Current.Play(videoUrl, MediaFileType.Video);
  24. PlayStopButtonText.Text = "Stop";
  25. }
  26. else if (PlayStopButtonText.Text == "Stop")
  27. {
  28. CrossMediaManager.Current.Play(videoUrl, MediaFileType.Video);
  29. PlayStopButtonText.Text = "Play";
  30. }
  31. }
  32. }
  33. }
Step 5

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.
Android Project

Add Android project by going to Solution Explorer >> VideoPlayerApp.Droid >>MainActivity.cs and double-click to open its design View. Here is the code.
MainActivity.cs page code
  1. using System;
  2. using Android.App;
  3. using Android.Content.PM;
  4. using Android.Runtime;
  5. using Android.Views;
  6. using Android.Widget;
  7. using Android.OS;
  8. using Plugin.MediaManager.Forms.Android;
  9. namespace VideoPlayerApp.Droid
  10. {
  11. [Activity(Label = "VideoPlayerApp", Icon = "@drawable/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
  12. public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
  13. {
  14. protected override void OnCreate(Bundle bundle)
  15. {
  16. TabLayoutResource = Resource.Layout.Tabbar;
  17. ToolbarResource = Resource.Layout.Toolbar;
  18. base.OnCreate(bundle);
  19. VideoViewRenderer.Init();//Nuget Packages oncreate mathod
  20. global::Xamarin.Forms.Forms.Init(this, bundle);
  21. LoadApplication(new App());
  22. }
  23. }
  24. }
IOS Project

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.
AppDelegate.cs page code
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Foundation;
  5. using Plugin.MediaManager.Forms.iOS;
  6. using UIKit;
  7. namespace VideoPlayerApp.iOS
  8. {
  9. // The UIApplicationDelegate for the application. This class is responsible for launching the
  10. // User Interface of the application, as well as listening (and optionally responding) to
  11. // application events from iOS.
  12. [Register("AppDelegate")]
  13. public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
  14. {
  15. //
  16. // This method is invoked when the application has loaded and is ready to run. In this
  17. // method you should instantiate the window, load the UI into it and then make the window
  18. // visible.
  19. //
  20. // You have 17 seconds to return from this method, or iOS will terminate your application.
  21. //
  22. public override bool FinishedLaunching(UIApplication app, NSDictionary options)
  23. {
  24. global::Xamarin.Forms.Forms.Init();
  25. LoadApplication(new App());
  26. VideoViewRenderer.Init();// nuget packages oncreate mathod
  27. return base.FinishedLaunching(app, options);
  28. }
  29. }
  30. }
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.
MainActivity.xaml.cs page code
  1. using Plugin.MediaManager.Forms.UWP;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Runtime.InteropServices.WindowsRuntime;
  7. using Windows.Foundation;
  8. using Windows.Foundation.Collections;
  9. using Windows.UI.Xaml;
  10. using Windows.UI.Xaml.Controls;
  11. using Windows.UI.Xaml.Controls.Primitives;
  12. using Windows.UI.Xaml.Data;
  13. using Windows.UI.Xaml.Input;
  14. using Windows.UI.Xaml.Media;
  15. using Windows.UI.Xaml.Navigation;
  16. namespace VideoPlayerApp.UWP
  17. {
  18. public sealed partial class MainPage
  19. {
  20. public MainPage()
  21. {
  22. this.InitializeComponent();
  23. VideoViewRenderer.Init();
  24. LoadApplication(new VideoPlayerApp.App());
  25. }
  26. }
  27. }
Step 6

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.