CommunityToolkit / Maui

The .NET MAUI Community Toolkit is a community-created library that contains .NET MAUI Extensions, Advanced UI/UX Controls, and Behaviors to help make your life as a .NET MAUI developer easier
https://learn.microsoft.com/dotnet/communitytoolkit/maui
MIT License
2.26k stars 397 forks source link

[Proposal] API that allows developers to hook a media element event and know the full screen status. #1927

Open Neotrickster opened 4 months ago

Neotrickster commented 4 months ago

Feature name

VideoPlayer_FullScreenStatusChanged

Link to discussion

https://github.com/CommunityToolkit/Maui/discussions/1748#discussioncomment-9548965

Progress tracker

Summary

API that allows developers to hook a media element event and know the full screen status. Is it full screen or normal ?

Motivation

In Xamarin times, we already have something to control when the Full Screen status changed (first and third party) and now we have a very mature Media Element in MAUI is about time to start with this.

Detailed Design

/// <summary>
/// Backing store for the <see cref="FullScreenState"/> property.
/// </summary>
public static readonly BindableProperty FullScreenProperty =
    BindableProperty.Create(nameof(FullScreenState), typeof(MediaElementScreenState), typeof(MediaElement), 
    MediaElementScreenState.Default, propertyChanged: OnFullScreenPropertyChanged);

Usage Syntax

public MediaElementPage(MediaElementViewModel viewModel, ILogger<MediaElementPage> logger) : base(viewModel)
{
    InitializeComponent();
    MediaElement.FullScreenStateChanged += MediaElement_FullScreenStateChanged;
}

void MediaElement_FullScreenStateChanged(object? sender, FullScreenStateChangedEventArgs e) =>
    logger.LogInformation("FullScreen State Changed. Old State: {PreviousState}, New State: {NewState}", e.PreviousState, e.NewState);

Drawbacks

I don't see any reason to not do it :)

Alternatives

Before I migrate to MAUI MediaElement I was using Xamarin with Xamarians/MediaPlayer [https://github.com/Xamarians/MediaPlayer] , looks simple enough from a use perspective:

public partial class SegnalesEnVivoPage : ContentPage
{
        InitializeComponent();
        BindingContext = new SegnalesEnVivoViewModel();

        VideoPlayer.FullScreenStatusChanged += VideoPlayer_FullScreenStatusChanged;
}

private void VideoPlayer_FullScreenStatusChanged(object sender, bool value)
{
            NavigationPage.SetHasNavigationBar(this, !value);
            Shell.SetNavBarIsVisible(this, !value);
            frmInformation.IsVisible = !value;
            frmBackground.IsVisible = !value;
            frmPlayer.Margin = value ? new Thickness(0, 0, 0, 0) : new Thickness(10, 20, 10, 10);
            VideoPlayer.HeightRequest = value ? deviceScreenHeight : 240;
            InvalidateMeasure();
        }

}

Unresolved Questions

Perhaps the above API is a good v1 if it works on all platforms? And then we iterate on more functionality in later versions?

ne0rrmatrix commented 4 months ago

That looks great! I have done the initial work to test and it does indeed work. I have it working for Android, Windows, iOS, and Mac Catalyst. So if we move ahead with this API all that is needed is for me to finish editing and then create PR and have it reviewed. Here is link to branch with required changes already done. https://github.com/ne0rrmatrix/MauiOld/tree/FullScreenEvents