xamarin.forms - Xamarin Forms:如何使用 Plugin.MediaManager.Forms 播放视频?
问题描述
我正在尝试使用播放视频Plugin.MediaManager.Forms
,我指的是这个博客。
第 1 步:添加Plugin.MediaManager
和Plugin.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.cs
和MainPage.xaml.cs
中。但是此初始化出现以下错误。
当前上下文中不存在名称“VideoViewRenderer”
我错过了什么吗?我检查了其他一些博客,但发生了同样的错误。我在这里添加了一个示例项目。
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 选项。
推荐阅读
- java - 在活动 android 中访问 SQLite 数据库值
- javascript - 如何在 Gatsby 中使用带有富文本的内嵌图像?
- python - 如何通过 Selenium 读取按钮内的值?
- javascript - 如何在客户端的 javascript / 中访问 Django 返回的 json 对象?
- python-3.x - 将现有的 matplotlib 图形放入子图中
- json - 解析复杂的 JSON,其中数据和“列标题”位于单独的数组中
- linux - 无法在 Kafka 开发盒上启动多个 Zookeeper 服务
- c# - 如何将 PHP pack('H40',$string); 转换为 C#?
- python - 椭圆曲线上的标量乘法
- javascript - 使用 await Promise.all 的 Javascript 语法