我有一个YouTube视频列表,我想在本机手机的媒体播放器上播放视频。我可以调用网络浏览器和youtube应用程序,但无法调用android / IOS手机的默认媒体播放器。
答案 0 :(得分:4)
这是在我的xamarin应用程序中实现媒体播放器的整个实现,该应用程序支持mvvm。
首先,您需要将插件媒体管理器(Nuget或GitHub)添加到您的三个项目(Portable,Droid和iOS)中。为此,您需要右键单击您的项目并单击Manage NuGet Packages选项并浏览插件
Plugin.MediaManager Plugin.MediaManager.Forms 安装这两个插件。
然后,您需要初始化VideoView,因此请将此代码添加到onCreate中Droid的MainActivity文件中。
protected override void OnCreate(Bundle bundle) { TabLayoutResource = Resource.Layout.Tabbar; ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(bundle);
VideoViewRenderer.Init();
global::Xamarin.Forms.Forms.Init(this, bundle);
FileAccessHelper.CopyDatabaseIfNotExists("BizQuiz");
LoadApplication(new App());
}
现在制作一个这样的视图。
这是我的xaml文件:MediaPlayer.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="Add your class name here"
xmlns:local="clr-namespace:MediaForms"
xmlns:forms="clr-
namespace:Plugin.MediaManager.Forms;assembly=Plugin.MediaManager.Forms"
BackgroundColor="Aqua">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="4*"/>
<RowDefinition Height="1*"/>
</Grid.RowDefinitions>
<forms:VideoView Grid.Row="0" Grid.RowSpan="1"
HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand"
Source="http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"
AspectMode="AspectFill"/>
<Grid HorizontalOptions="FillAndExpand" Grid.Row="1" Grid.RowSpan="1"
Margin="20">
<Grid.RowDefinitions>
<RowDefinition Height="1*"/>
<RowDefinition Height="6*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<StackLayout Grid.Row="0" Grid.ColumnSpan="3"
Orientation="Vertical">
<StackLayout Orientation="Horizontal">
<Label Text="Duration: "></Label>
<Label x:Name="Duration"/>
</StackLayout>
<ProgressBar x:Name="ProgressBar" Grid.ColumnSpan="3"
HorizontalOptions="FillAndExpand"></ProgressBar>
</StackLayout>
<Button Grid.Row="1" Grid.Column="0" TextColor="White"
BackgroundColor="Gray" HeightRequest="50" VerticalOptions="Center"
Text="Play" WidthRequest="100" Clicked="PlayClicked"></Button>
<Button Grid.Row="1" Grid.Column="1" TextColor="White" Text="Pause"
BackgroundColor="Gray" HeightRequest="50" VerticalOptions="Center"
WidthRequest="100" Clicked="PauseClicked"></Button>
<Button Grid.Row="1" Grid.Column="2" TextColor="White" Text="Stop"
BackgroundColor="Gray" HeightRequest="50" VerticalOptions="Center"
WidthRequest="100" Clicked="StopClicked"></Button>
</Grid>
</Grid>
</ContentPage>`
这是我的xaml.cs文件
MediaPLayer.xaml.cs
public partial class MediaPlayer : ContentPage
{
private IPlaybackController PlaybackController =>
CrossMediaManager.Current.PlaybackController;
public MediaPlayer()
{
InitializeComponent();
CrossMediaManager.Current.PlayingChanged += (sender, e) =>
{
ProgressBar.Progress = e.Progress;
Duration.Text = "" + e.Duration.TotalSeconds.ToString() + "
seconds";
};
}
void PlayClicked(object sender, System.EventArgs e)
{
PlaybackController.Play();
}
void PauseClicked(object sender, System.EventArgs e)
{
PlaybackController.Pause();
}
void StopClicked(object sender, System.EventArgs e)
{
PlaybackController.Stop();
}
}
这就是全部,你可以像这样从URL播放视频。