我对Xamarin来说很新......
我正在尝试创建像app这样的YouTube。我不知道如何在我的应用程序内播放视频。我曾尝试使用MediaManager插件https://github.com/martijn00/XamarinMediaManager,但根本无法搞清楚。
如果有人可以推荐不同的插件,或者解释IN DETAIL如何使用MediaManager插件,那就太棒了。
插件上的文档非常糟糕,根本无法理解。
到目前为止我所拥有的内容:我只是想通过点击按钮来播放视频。
Xaml文件:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="App2.Video_Play_Page"
Title="Video_Play_Page"
BackgroundColor="#4B1388">
<ContentPage.Content>
<StackLayout Padding="10, 10, 10, 10">
<Label Text="video Player..." />
<Button Clicked="PlayVideo" Text="Play me!"/>
</StackLayout>
</ContentPage.Content>
Xaml.cs文件:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using Plugin.MediaManager;
namespace App2
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class Video_Play_Page : ContentPage
{
public Video_Play_Page()
{
InitializeComponent();
}
private void PlayVideo(object sender, EventArgs e)
{
CrossMediaManager.Current.Play("https://www.youtube.com/watch?v=Gm8bQxnold0");
}
}
}
注意:我不知道我是否正确安装了所有内容。如果有人可以解释一下,它也会很棒。
答案 0 :(得分:1)
您应该在Xaml文件中放置表单:VideoView 以下是在Xamarin.Forms中使用它的示例,请检查此项 https://github.com/martijn00/XamarinMediaManager/tree/develop/Samples/Forms
答案 1 :(得分:1)
你几乎拥有一切。
你需要像
那样实例化播放器在.xaml文件中
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="App2.Video_Play_Page"
Title="Video_Play_Page"
BackgroundColor="#4B1388"
xmlns:forms="clr-namespace:Plugin.MediaManager.Forms;assembly=Plugin.MediaManager.Forms">
<ContentPage.Content>
<StackLayout Padding="10, 10, 10, 10">
<Label Text="video Player..." />
<forms:VideoView />
<Button Clicked="PlayVideo" Text="Play me!"/>
</StackLayout>
</ContentPage.Content>
或.xaml.cs文件
new VideoView { };
但是使用您的代码我建议使用第一个选项。
然后你会对插件说你要重现视频而不是音频。
private void PlayVideo(object sender, EventArgs e)
{
CrossMediaManager.Current.Play("https://www.youtube.com/watch?v=Gm8bQxnold0", MediaFileType.Video);
}
如果插件使用youtube网址重现视频,我不会感到害羞,如果这不起作用,你应该用绝对网址替换网址
https://sec.ch9.ms/ch9/37af/240037cc-e74a-421a-9946-7ce4205d37af/DiAndIocForXamarinForms.mp4
答案 2 :(得分:0)
我建议查看 Xamarin.Community.Toolkit
这是一个跨平台的视频播放器,在以下位置有详细记录:
https://docs.microsoft.com/en-us/xamarin/community-toolkit/views/mediaelement
您可以像在 Xamarin 中创建的任何其他控件一样,在 C# 中生成 MediaElement。它的工作方式与 Image
控件非常相似:允许您设置宽高比、是否显示平台默认视频控件等...
如果您是 Xamarin 的新手,请看上面链接中的示例:
<MediaElement Source="https://sec.ch9.ms/ch9/5d93/a1eab4bf-3288-4faf-81c4-294402a85d93/XamarinShow_mid.mp4"
ShowsPlaybackControls="True" />
C# 中的等价物:
MediaElement videoPlayer = new MediaElement
{
Source = "https://sec.ch9.ms/ch9/5d93/a1eab4bf-3288-4faf-81c4294402a85d93/XamarinShow_mid.mp4",
ShowsPlaybackControls = false
};
可以像添加 Label
、Button
或任何其他继承 View
类的控件一样将其添加到任何视图中。
可以在此处找到 Gerald Versluis(Xamarin 团队的成员)提供的方便教程,其中详细介绍了您可以使用该插件执行的所有操作(以及一些常见错误以及如何修复它们):