4

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?

Daniel Simon
  • 53
  • 1
  • 6
  • Make sure that you had added permissions in Android project . – Lucas Zhang May 11 '20 at 13:52
  • @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 May 12 '20 at 06:21

2 Answers2

4

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>
Leo Zhu
  • 15,726
  • 1
  • 7
  • 23
  • I am not embedding my mp4 in the App, I am accessing the file from an URL. I already set the Experimental flag in the App.xaml.cs. – Daniel Simon May 12 '20 at 07:41
  • @DanielSimon I change the update above,if you want to play a file from an Url,you just need change the Source to your url. – Leo Zhu May 12 '20 at 08:24
  • Your solution works with your mp4 file, although the playback of the file starts pretty late. But it doesn't work with my mp4 file. I wait a few minutes, but nothing happens. The file is not broken, in iOS it works with no problems, the playback starts also a lot faster under iOS. – Daniel Simon May 15 '20 at 19:59
  • @DanielSimon You could try to use the url in my sample to see if it would work. – Leo Zhu May 19 '20 at 01:14
  • Please check this app -->https://github.com/devcrux/Xamarin.Forms-Shapes ,I have done all the instructions provides.It works perectly in android 9 but not working with android 10. – XamarinInfo Feb 16 '22 at 07:09
1

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.