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.19k stars 375 forks source link

[BUG] MediaElement Android crashes when OnFullscreenButtonClick #2044

Open vikher opened 1 month ago

vikher commented 1 month ago

Is there an existing issue for this?

Did you read the "Reporting a bug" section on Contributing file?

Current Behavior

The Android app crashes when the fullscreen button is clicked.

Expected Behavior

The app should not crash when the fullscreen button is clicked.

Steps To Reproduce

Use MediaElement version 3.1.0. Click the fullscreen button mainly on a Galaxy S23 Ultra, Galaxy S22 Ultra, or Pixel 8 Pro.

Link to public reproduction project repository

https://github.com/vikher/testmedia

Environment

- .NET MAUI CommunityToolkit Version 9.0.2
- OS: net8.0-android
- .NET MAUI: MauiVersion 8.0.70
- CommunityToolkit.Maui.MediaElement Version 3.1.0

Anything else?

  1. Version 3.1.1 breaks fullscreen functionality.
  2. Versions 4.0+ causes the app to reopen on the last visited page and continue background audio, behavior that is not desired.
  3. Awaiting approval of CommunityToolkit/Maui PR #2039 to use the latest NuGet package.

Error Details:

Error: System.ObjectDisposedException Message: ObjectDisposed_Generic ObjectDisposed_ObjectName_Name, CommunityToolkit.Maui.Core.Views.MauiMediaElement

Xamarin Exception Stack:
System.ObjectDisposedException: ObjectDisposed_Generic
ObjectDisposed_ObjectName_Name, CommunityToolkit.Maui.Core.Views.MauiMediaElement
  at Java.Interop.JniPeerMembers.AssertSelf(IJavaPeerable )
  at Java.Interop.JniPeerMembers.JniInstanceMethods.InvokeVirtualVoidMethod(String , IJavaPeerable , JniArgumentValue* )
  at Android.Views.ViewGroup.AddView(View )
  at CommunityToolkit.Maui.Core.Views.MauiMediaElement.OnFullscreenButtonClick(Object sender, FullscreenButtonClickEventArgs e)
  at Com.Google.Android.Exoplayer2.UI.StyledPlayerView.IFullscreenButtonClickListenerImplementor.OnFullscreenButtonClick(Boolean isFullScreen)
  at Com.Google.Android.Exoplayer2.UI.StyledPlayerView.IFullscreenButtonClickListenerInvoker.n_OnFullscreenButtonClick_Z(IntPtr jnienv, IntPtr native__this, Boolean isFullScreen)
  at Android.Runtime.DynamicMethodNameCounter.24(IntPtr , IntPtr , Boolean )

Code Context:

I'm using DisconnectMediaElementHandler method to dispose the media element, based on this discussion:

  public override void Stop()
  {
      base.Stop();
      DisconnectMediaElementHandler();
  }

private void DisconnectMediaElementHandler()
{
    mediaElement.Handler?.DisconnectHandler();
    mediaElement.Dispose();
}

The DisconnectMediaElementHandler method is executed when the page disappears, not when the fullscreen button is clicked, at least on Android.

I'm not using ContentPage_Unloaded because, on iOS, when the fullscreen button is clicked and it enters fullscreen mode, CnkContentPage_Unloaded gets called and disposes of the media element view.

        //DO NOT DO THIS IN IOS
        void ContentPage_Unloaded(System.Object sender, System.EventArgs e)
        {
            DisconnectMediaElementHandler();
        }

Additional Information: There is no current way to trigger a specialized event in Maui when the fullscreen button is clicked.

The following screenshot from the app center image

ne0rrmatrix commented 1 month ago

There is a PR that came out last night that will add support for full screen event handling. https://github.com/CommunityToolkit/Maui/pull/2041

ne0rrmatrix commented 4 weeks ago

I tested against current main repo for MediaElement and toolkit. Could not reproduce issue. Will keep this open till #2041 is merged.

ne0rrmatrix commented 2 weeks ago

Can you try the latest version? 4.1.0 Came out last week.