如何以Xamarin形式调用媒体播放器 [英] how to invoke media player in Xamarin forms
问题描述
我有一个YouTube视频列表,我想在本机手机的媒体播放器上播放视频.我能够调用网络浏览器和youtube应用,但不能调用android/IOS手机的默认媒体播放器.
i have a list of YouTube videos and i want to play videos on media player of native mobile phone.i am able to invoke web browsers and youtube app but cant invoke default mediaplayer of android/IOS phone.
推荐答案
这是在具有mvvm支持的xamarin应用中实现媒体播放器的全部实现.
This is the whole implementation made to implement media player in my xamarin app which is having mvvm support.
-
首先,您需要添加插件Media Manager( Nuget 或 GitHub )添加到您的三个项目(便携式,Droid和iOS).为此,您需要右键单击您的项目,然后单击管理NuGet软件包"选项并浏览插件
First of all, you need to add the plugin Media Manager ( Nuget or GitHub ) to three of your projects(Portable, Droid and iOS). For that, you need to right-click on your project and click on the Manage NuGet Packages option and browse for plugins
Plugin.MediaManager Plugin.MediaManager.Forms 安装这两个插件.
Plugin.MediaManager Plugin.MediaManager.Forms Install both these plugins.
然后,您需要初始化VideoView,因此将此代码添加到onCreate中Droid的MainActivity文件中.
Then, you need to initialize VideoView, so add this code to your MainActivity file of Droid in onCreate.
受保护的替代无效OnCreate(捆绑包) { TabLayoutResource = Resource.Layout.Tabbar; ToolbarResource = Resource.Layout.Toolbar;
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
This is my xaml file: 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文件
And this is my xaml.cs file
MediaPLayer.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播放视频.
That's all, You can play the video from URL like this.
这篇关于如何以Xamarin形式调用媒体播放器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!