Xamarin Forms:如何使用 Plugin.MediaManager.Forms 播放视频?

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

我正在尝试使用 Plugin.MediaManager.Forms 播放视频,我指的是这个 blog

步骤 1: 添加了 Plugin.MediaManagerPlugin.MediaManager.Forms

第 2 步: XAML 代码 - 添加了 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>   

第 3 步:xaml.cs代码

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";
            }
        }
    }

但是在这一步出现错误:

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

第 4 步: 还在 MainActivity.csAppDelegate.csMainPage.xaml.cs 中添加了 VideoViewRenderer.Init();。但是此初始化出现以下错误。

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

我错过了什么吗?我检查了其他一些博客,但发生了同样的错误。我添加了一个示例项目 here.

Android 选项截图:

该博客似乎已过时。部分 API 和方法已废弃。您应该查看来自 https://github.com/martijn00/XamarinMediaManager#usage 的最新文档。

使用以下代码代替 VideoViewRenderer.Init() ;

CrossMediaManager.Current.Init();

并且只需要调用方法

CrossMediaManager.Current.Play(videoUrl);

并且我查看了您的演示。您需要在共享项目和特定平台(Android 和 iOS)中将 Xamarin.Forms 的版本更新为 4.2.x。这将与插件的版本相匹配.

不要忘记将 Dex 编译器设置为 d8

右键单击您的 Android 项目 -> 属性-> Android 选项。