1

I want to substitute XCT:MediaElement with Plugin.MediaManger & Plugin.MediaManager.Forms for my videos to play cross-platform. Documentation I've read this like 5 times and it still doesn't make any sense. I'm putting my video into Assets in Android and using XAML code below. The picture is the output I get on the screen, it's just a black background which I can change to any color...

I've added the Init() to the MainActivity and also to the AppDelegate. Followed everything step by step.

What really doesn't make any sense from the documentation is (Examples). CrossMediaManager is a static class. I cannot create a new instance of static classes so I cannot actually assign anything to the layout. How does it suppose to play anything on the screen if there is nothing that can actually consume this?

await CrossMediaManager.Current.PlayFromAssembly("somefile.mp3", typeof(BaseViewModel).Assembly);
await CrossMediaManager.Current.PlayFromResource("assets:///somefile.mp3");
await CrossMediaManager.Android.PlayFromResource(Resource.Raw.somefile.ToString());

Further, on the documentation you come across the Xamarin.Forms part where it shows how to do this is XAML with mm:ViewView now this makes more sense and I can also assign an x:Name to it and pass through MVVM pattern through the Behind Code to the ViewModel and so on, but this would be cool if the video played in the first place...

Any idea why this is happening?
Many Thanks in Advance.

Nuget Packges I've added: Plugin.MediaManager & Plugin.MediaManager.Forms

<StackLayout BackgroundColor="Red"
                HorizontalOptions="CenterAndExpand"
                VerticalOptions="CenterAndExpand"
                WidthRequest="400"
                HeightRequest="600"
                Margin="10"
                Padding="20">
    <mm:VideoView VerticalOptions="FillAndExpand"
                    Source="assets:///pea2.mp4"
                    AutoPlay="True"
                    ShowControls="True" />
</StackLayout>

enter image description here

  • https://github.com/Baseflow/XamarinMediaManager/issues/840 – Jason Dec 14 '22 at 14:15
  • @Jason okay so I've read that but still not of any help as it doesn't say how on earth a static class binds to the view and plays a video...? `await CrossMediaManager.Current.Play("file:///android_asset/long-test.mp3");` I'm awaiting a IMediaItem here that contains `MetaData`... I don't want `MetaData` I want to see the video play on the screen. – ThisQRequiresASpecialist Dec 14 '22 at 14:27
  • Have you looked at the samples? Or read https://github.com/Baseflow/XamarinMediaManager#add-video-player-to-the-ui – Jason Dec 14 '22 at 15:07
  • 1
    I have test it on the android 11, the vedio can play successfully. But on the andorid 12, I met the same problem as yours. And on the ios, the vedio control view will show but the vedio didn't display. And there is [an issue about the plugin doesn't work on the android 12](https://github.com/Baseflow/XamarinMediaManager/issues/876). – Liyun Zhang - MSFT Dec 15 '22 at 09:00
  • @LiyunZhang-MSFT thanks for that, but at this point, I don't even understand how the whole thing works at all. I've spent last night 3 hours trying to figure out how the whole things binds together and I just don't understand what on earth is going on with this `Plugin`. Is it possible you can write and answer of step by step of how to get it to work because after investing [AndroidSamples](https://github.com/Baseflow/XamarinMediaManager/tree/develop/Samples/AndroidPlayerSample) I got even more confused... – ThisQRequiresASpecialist Dec 15 '22 at 09:05
  • I didn't try the local mp4 file, I tested it with a mp4 file in the website. – Liyun Zhang - MSFT Dec 15 '22 at 09:08
  • I will take that if possible, I will figure out the rest I just want to see the video play in the first place and after that, I will start to break it down from top to bottom. Otherwise, I'm just hitting a brick wall ATM. Many Thanks – ThisQRequiresASpecialist Dec 15 '22 at 09:09

1 Answers1

0

At first, I install the Plugin.MediaManger & Plugin.MediaManager.Forms two packages in all the project, such as PCL(forms part), android part and ios part.

And then I added the following code in the MainActivity:

    protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            Xamarin.Essentials.Platform.Init(this, savedInstanceState);
            global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
                  CrossMediaManager.Current.Init(this);
            CrossMediaManager.Current.AutoPlay = true;
            LoadApplication(new App());
        }

And in the MainPage.Xaml:

    <StackLayout>
        <mm:VideoView VideoAspect="AspectFit" VerticalOptions="FillAndExpand" Source="https://www.appsloveworld.com/wp-content/uploads/2018/10/sample-mp4-video.mp4" ShowControls="True"/>
    </StackLayout>

And here is the result image on the android 11 emulator:

enter image description here

Note, according to the issue I mentioned in the comments, the plugin will work on the android 12 after adding the CrossMediaManager.Current.Speed = 1F;. But it will throw exception when I added it. It's strange.

Liyun Zhang - MSFT
  • 8,271
  • 1
  • 2
  • 14
  • I'm running into an identical issue. Big F. Do you potentially know of any other packages for `Xamarin.Forms` that play videos on both platforms without flashing black on loading the video to the screen like `Xamarin.Communitytoolkit`. Sorry for long response focused on developer other aspects and not be stuck on this. – ThisQRequiresASpecialist Dec 20 '22 at 09:20
  • All right, did you try to use the [mediaelement](https://learn.microsoft.com/en-us/xamarin/community-toolkit/views/mediaelement). @ThisQRequiresASpecialist – Liyun Zhang - MSFT Dec 20 '22 at 09:26
  • Yeah, `MediaElement` is the one that causes all the issues for me. It keeps blinking black when loading a new screen. That's why I tried finding a new package. Unless you maybe could help me figure out how I can pre-load a video before the next screen is shown. I know the exact sequence in which videos will play. – ThisQRequiresASpecialist Dec 20 '22 at 09:28
  • What does `blinking black when loading a new screen` mean? Did you try to use the custom renderer to get the effect you want? @ThisQRequiresASpecialist – Liyun Zhang - MSFT Dec 20 '22 at 09:43
  • What I mean is that once a new screen load the video player stars flashing black for 1 seconds before the video plays. Or the video stutters loads of times and just constantly restarts and IDK why but if I have more than 1 `MediaElement` they don't actually listen correctly to their properties. I've made a [post](https://stackoverflow.com/questions/74138061/xamarin-community-toolkit-mediaelement-flashes-black-on-load-android-problem-onl) about this. I will try 1 more thing right now by creating a `VideoLoader` to load the next video into memory before page shows. – ThisQRequiresASpecialist Dec 20 '22 at 09:47
  • You can try to use the platforms' player to display the video with the dependency service. – Liyun Zhang - MSFT Dec 22 '22 at 09:44