Forum Xamarin.Forms

Media Element crash when navigating away from page on Ios

az6bcnaz6bcn Member

I am getting Media Element disposing exception when I navigate back to a previous page from a page where I have my Media Element.

The managed Managed Stacktrace:

          at <unknown> <0xffffffff>
          at ObjCRuntime.Messaging:void_objc_msgSend_IntPtr <0x00007>
          at AVFoundation.AVPlayer:ReplaceCurrentItemWithPlayerItem <0x0005b>
          at Xamarin.Forms.Platform.iOS.MediaElementRenderer:Dispose <0x002b7>
          at Foundation.NSObject:Dispose <0x00023>

My steps to reproduce were:
Push new page with MediaElement. Click play on video.
Click back to pop page.
Page does pop but UI becomes unresponsive.

my code is:

public partial class SermonsMediaPlayer : ContentPage
            {
                private Sermon Sermon;

                public SermonsMediaPlayer(Sermon selectedSermon)
                {
                    InitializeComponent();

                    if (selectedSermon is null)
                    {
                        Sermon = new Sermon();
                    }

                    Sermon = selectedSermon;
                    BindingContext = Sermon;

                }

                async void Back_Clicked(object sender, EventArgs args)
                {
                    // release media resources
                    if (mediaPlayer.CurrentState == MediaElementState.Playing)
                    {

                        mediaPlayer.Stop();
                        await Navigation.PopModalAsync();
                    }

                    await Navigation.PopModalAsync();
                }

                protected override void OnDisappearing()
                {
                    base.OnDisappearing();
                }
            }


        <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="xxxViews.SermonsMediaPlayer">
            <ContentPage.ToolbarItems>
                <ToolbarItem Text="Back" Clicked="Back_Clicked" />
            </ContentPage.ToolbarItems>
            <ContentPage.Content>
                <Grid>
                    <MediaElement x:Name="mediaPlayer" Source="{Binding Url}" AutoPlay="False"
                      ShowsPlaybackControls="True" KeepScreenOn="False" />
                </Grid>
            </ContentPage.Content>
        </ContentPage>

Answers

  • ColeXColeX Member, Xamarin Team Xamurai

    I test with your code but can't be able to reproduce the problem .

    Would you mind sharing a basic, minimum project to us ?

    Just zip the project and attach in the comment .

  • jennietjenniet Member

    I'm seeing the same problem, but only intermittently.

  • az6bcnaz6bcn Member

    I managed to fixed this by changing
    _rateObserver.Dispose();

    to this:
    _avPlayerViewController?.Player?.RemoveObserver(_rateObserver, "rate");

    in the MediaElement renderer for iOS.

  • jennietjenniet Member

    How does one access the MediaElement renderer for iOS please?
    Or should I just wait for the problem to be fixed in Xamarin?

  • ionixjuniorionixjunior USMember ✭✭✭
    edited August 2020

    Really it works @az6bcn, thank you!

    @jenniet unfortunately you need copy entirely MediaElement iOS custom renderer from Xamarin.Forms repo https://github.com/xamarin/Xamarin.Forms/blob/main/Xamarin.Forms.Platform.iOS/Renderers/MediaElementRenderer.cs

    Looks like at moment we don't possible to extend the MediaElement iOS renderer to override a simple method to do own behaviour because is sealed one. Maybe in the future it change.

  • KarlSKarlS Member ✭✭

    I am having this issue, anyone have a source code/ example I can refer to for the fix, not too sure how to implement MediaElement iOS custom renderer

  • KarlSKarlS Member ✭✭

    https://docs.microsoft.com/en-us/xamarin/xamarin-forms/release-notes/5.0/5.0.0-pre1

    So in this new update, we have to use the toolkit for media element, however does this resolve he IOS issue.?

  • ionixjuniorionixjunior USMember ✭✭✭

    @KarlS this don't solve the issue in iOS yet, but will be solved in the future 😉

  • findjammerfindjammer Member ✭✭

    Hmm ... seems copying the latest renderer is now a no go due to Internal classes like SeekRequested.

    Seems the code has changed drastically since this fix was published as well.

    Has anyone worked on this with the latest renderer that also now isn't sealed.

Sign In or Register to comment.