Search code examples
xamarin.formsvideo-player

Xamarin Forms: How to play Video using Plugin.MediaManager.Forms?


I am trying to play a video using Plugin.MediaManager.Forms and I am referring to this blog.

Step 1: Added Plugin.MediaManager and Plugin.MediaManager.Forms.

Step 2: XAML code - Added a VideoView

<?xml version="1.0" encoding="utf-8" ?>  
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"  
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"  
             xmlns:local="clr-namespace:VideoPlayerApp"  
             x:Class="VideoPlayerApp.MainPage"  
             xmlns:forms="clr-namespace:Plugin.MediaManager.Forms;assembly=Plugin.MediaManager.Forms"  
             Title="Video Player">  

    <ContentPage.Content>  
        <StackLayout>  

            <Label Text="Xamarin Forms"  
                   FontSize="40"  
                   TextColor="Azure"/>  
            <Label Text="Video Player Application"  
                   FontSize="58"  
                   TextColor="BlueViolet"/>  
            <Button x:Name="PlayStopButtonText"  
                    Text="Play"   
                    Clicked="PlayStopButton"  
                    TextColor="BlueViolet"/>  
            <forms:VideoView HeightRequest="202"  
                             WidthRequest="202"/>  
        </StackLayout>  
    </ContentPage.Content>
</ContentPage>   

Step 3: xaml.cs code

public partial class MainPage : ContentPage
    {
        private string videoUrl = "https://sec.ch9.ms/ch9/e68c/690eebb1-797a-40ef-a841-c63dded4e68c/Cognitive-Services-Emotion_high.mp4";
        public MainPage()
        {
            InitializeComponent();
        }

        private void PlayStopButton(object sender, EventArgs e)
        {
            if (PlayStopButtonText.Text == "Play")
            {
                CrossMediaManager.Current.Play(videoUrl, MediaFileType.Video);

                PlayStopButtonText.Text = "Stop";
            }
            else if (PlayStopButtonText.Text == "Stop")
            {
                CrossMediaManager.Current.Stop();

                PlayStopButtonText.Text = "Play";
            }
        }
    }

But getting error on this step:

Error CS0103 The name 'MediaFileType' does not exist in the current context

Step 4: Also added VideoViewRenderer.Init();in MainActivity.cs, AppDelegate.cs and MainPage.xaml.cs. But getting following error for this initialization.

The name 'VideoViewRenderer' does not exist in the current context

Am I missing something? I checked some other blogs but same error occuring. I have added a sample project here.

Android Options Screenshot:

enter image description here


Solution

  • The blog seems out of date. Part of APIs and methods were obsoleted . You should check the newest docs from https://github.com/martijn00/XamarinMediaManager#usage .

    use the following code instead of VideoViewRenderer.Init() ;

    CrossMediaManager.Current.Init();
    

    And just need to call the method

    CrossMediaManager.Current.Play(videoUrl);
    

    And I checked your demo . You need to update the version of Xamarin.Forms to 4.2.x both in share project and specific platforms(Android and iOS) .Which will match to the version of the plugin.

    Don't forget to set the Dex complier to d8 .

    Right click your Android project -> Property-> Android Options.

    enter image description here