Search code examples
androidxamarin.formsmediaelementxamarin-forms-4

New Xamarin Forms 4.6 MediaElement not working for Android


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?


Solution

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