xamarin / XamarinCommunityToolkit

The Xamarin Community Toolkit is a collection of Animations, Behaviors, Converters, and Effects for mobile development with Xamarin.Forms. It simplifies and demonstrates common developer tasks building iOS, Android, and UWP apps with Xamarin.Forms.
MIT License
1.59k stars 475 forks source link

[Bug] Android NullReferenceException GetMetaData #997

Open mknotzer opened 3 years ago

mknotzer commented 3 years ago

On Startup MediaElement (Source not set) causes an unhandled Exception:

{System.NullReferenceException: Object reference not set to an instance of an object. at Xamarin.CommunityToolkit.UI.Views.FormsVideoView+<>c__DisplayClass7_0.b__0 () [0x00006] in <08abc6c084af4622851290c7f2f974ff>:0 at System.Threading.Tasks.…}

at Xamarin.CommunityToolkit.UI.Views.FormsVideoView+<>c__DisplayClass7_0.b__0 () [0x00006] in <08abc6c084af4622851290c7f2f974ff>:0 at System.Threading.Tasks.Task.InnerInvoke () [0x0000f] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/external/corert/src/System.Private.CoreLib/src/System/Threading/Tasks/Task.cs:2476 at System.Threading.Tasks.Task.Execute () [0x00000] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/external/corert/src/System.Private.CoreLib/src/System/Threading/Tasks/Task.cs:2319

Xamarin Toolkit 1.0.3

The view works later despite the exception, playing a video is possible.

AmrAlSayed0 commented 3 years ago

The only thing that can be null in this function is the Uri itself https://github.com/xamarin/XamarinCommunityToolkit/blob/main/src/CommunityToolkit/Xamarin.CommunityToolkit/Views/MediaElement/Android/FormsVideoView.android.cs#L59-L78

mknotzer commented 3 years ago

the source is not set in the XAML, so why would MediaElement call GetMediaData?

<toolkit:MediaElement x:Name="xVideo" AutoPlay="false" BackgroundColor="Black" HeightRequest="300" HorizontalOptions="Center" ShowsPlaybackControls="False" WidthRequest="500" />

AmrAlSayed0 commented 3 years ago

It's probably this https://github.com/xamarin/XamarinCommunityToolkit/blob/0db93526c5c973044c282aa6d14657fcc13e48ea/src/CommunityToolkit/Xamarin.CommunityToolkit/Views/MediaElement/Android/MediaElementRenderer.android.cs#L345 Bind Source to a dummy value before switching it to the real value and see if that fixes the issue.

mknotzer commented 3 years ago

issue still exists on Android.

<toolkit:MediaElement x:Name="xVideo" AutoPlay="false" BackgroundColor="Black" HeightRequest="300" HorizontalOptions="Center" ShowsPlaybackControls="False" Source="{Binding Source, Source={x:Reference xThis}}" WidthRequest="500" />

Later use of

Source = new Uri(...)

not successful either (iOS and Android), have to use

xVideo.Source = new Uri(...)

instead.

pictos commented 3 years ago

@mknotzer Could you attach a small repro?