4
votes

I tried out the new Xamarin Forms 4.6.0.726 MediaElement control in a very simple Xamarin Forms Shell Project. I added the MediaElement control in a ContentPage and set its properties (AutoPlay on true, IsLooping on true, Source on an mp4 file, ShowPlaybackControls on true). I added also the Experimental-Flag for the MediaElement in the App.xaml.cs.

When I run the App, the video is played, with sound, image and player controls visible, on iOS, but it does not work on Android. On Android the player controls are not displayed, nothing happens.

Anyone else had this problem?

2
Make sure that you had added permissions in Android project .Lucas Zhang
@LucasZhang-MSFT - Do you mean the SetFlag MediaElement_Experimental in the MainActivity? I tried that already, does not make any difference. If you mean something else, please provide me some more details.Daniel Simon

2 Answers

4
votes

You could try to check these,make sure you have stored a media file in the platform project.

On Android, media files must be stored in a subfolder of Resources named raw. The raw folder cannot contain subfolders. The media file must have a Build Action of AndroidResource.

enter image description here

then in your page.xaml (don't use layout to wrap MediaElement):

<?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="MediaElementDemos.PlayAppPackageVideoResourcePage"
         Title="Play app package video resource">
    <MediaElement Source="ms-appx:///XamarinForms101UsingEmbeddedImages.mp4"
              ShowsPlaybackControls="True" IsLooping="True" AutoPlay="True" />
</ContentPage>

add Device.SetFlags(new string[] { "MediaElement_Experimental" }); in your App.xaml.cs

public App()
    {
        Device.SetFlags(new string[] { "MediaElement_Experimental" });
        InitializeComponent();
        MainPage = new NavigationPage(new PlayPage());
    }

Update:

If you want to play the mp4 from an URL.

<?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="MediaElementDemos.PlayAppPackageVideoResourcePage"
     Title="Play app package video resource">
    <MediaElement Source="https://sec.ch9.ms/ch9/5d93/a1eab4bf-3288-4faf-81c4-294402a85d93/XamarinShow_mid.mp4"
          ShowsPlaybackControls="True" IsLooping="True" AutoPlay="True" />
</ContentPage>
1
votes

Something I had to add according to this article on top of the above answers to make it work : https://github.com/xamarin/Xamarin.Forms/issues/9785

Device.StartTimer(TimeSpan.FromSeconds(1), () =>
            {
                videoPlayer.Play();

                videoPlayer.ScaleTo(0.99f);
                videoPlayer.ScaleTo(1.00f);

                return false;
            });

Make sure you add the above answers prior adding this.