how to invoke media player in Xamarin forms

2019-06-14 16:38发布

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.

1条回答
倾城 Initia
2楼-- · 2019-06-14 17:24

This is the whole implementation made to implement media player in my xamarin app which is having mvvm support.

  1. 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 Install both these plugins.

  2. Then, you need to initialize VideoView, so add this code to your MainActivity file of Droid in onCreate.

    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());
      }
    

Now make one view like this.

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>`

And this is my xaml.cs file

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();
    }
  }

That's all, You can play the video from URL like this.

查看更多
登录 后发表回答