【问题标题】:how to invoke media player in Xamarin forms如何在 Xamarin 表单中调用媒体播放器
【发布时间】:2017-07-06 15:39:38
【问题描述】:

我有一个 YouTube 视频列表,我想在本地手机的媒体播放器上播放视频。我可以调用网络浏览器和 youtube 应用程序,但无法调用 android/IOS 手机的默认媒体播放器。

【问题讨论】:

    标签: c# visual-studio xaml xamarin cross-platform


    【解决方案1】:

    这是在我的支持 mvvm 的 xamarin 应用程序中实现媒体播放器的整个实现。

    1. 首先,您需要将插件媒体管理器(NugetGitHub)添加到您的三个项目(Portable、Droid 和 iOS)中。为此,您需要右键单击您的项目并单击“管理 NuGet 包”选项并浏览插件

      Plugin.MediaManager Plugin.MediaManager.Forms 安装这两个插件。

    2. 然后,你需要初始化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 播放视频。

    【讨论】:

    • 这为我节省了很多时间
    • 我需要添加哪个程序集才能使用 VideoViewRenderer.Init()?快把我逼疯了
    • 如前所述,您只需从 nuget 管理器添加 Plugin.MediaManager Plugin.MediaManager.Forms 插件。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多