首页 > 解决方案 > Xamarin Forms:如何使用 Plugin.MediaManager.Forms 播放视频?

问题描述

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

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

但在这一步出错:

错误 CS0103 当前上下文中不存在名称“MediaFileType”

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

当前上下文中不存在名称“VideoViewRenderer”

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

Android 选项截图:

在此处输入图像描述

标签: xamarin.formsvideo-player

解决方案


博客似乎过时了。部分 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 选项。

在此处输入图像描述


推荐阅读