diff --git a/src/CommunityToolkit/Xamarin.CommunityToolkit/Views/MediaElement/Android/FormsVideoView.android.cs b/src/CommunityToolkit/Xamarin.CommunityToolkit/Views/MediaElement/Android/FormsVideoView.android.cs index 8c2c1711c..2bc14a908 100644 --- a/src/CommunityToolkit/Xamarin.CommunityToolkit/Views/MediaElement/Android/FormsVideoView.android.cs +++ b/src/CommunityToolkit/Xamarin.CommunityToolkit/Views/MediaElement/Android/FormsVideoView.android.cs @@ -52,10 +52,14 @@ protected void ExtractMetadata(MediaMetadataRetriever retriever) public override async void SetVideoURI(global::Android.Net.Uri? uri, IDictionary? headers) { + base.SetVideoURI(uri, headers); + + // this instance could get disposed during awaiting, so a call to the base method (AFTER awaiting) + // would throw ObjectDisposedException and be impossible to catch due to async void if (uri != null) + { await SetMetadata(uri, headers); - - base.SetVideoURI(uri, headers); + } } protected async Task SetMetadata(global::Android.Net.Uri uri, IDictionary? headers) diff --git a/src/CommunityToolkit/Xamarin.CommunityToolkit/Views/MediaElement/Android/MediaElementRenderer.android.cs b/src/CommunityToolkit/Xamarin.CommunityToolkit/Views/MediaElement/Android/MediaElementRenderer.android.cs index a7383894d..fcd7f0796 100644 --- a/src/CommunityToolkit/Xamarin.CommunityToolkit/Views/MediaElement/Android/MediaElementRenderer.android.cs +++ b/src/CommunityToolkit/Xamarin.CommunityToolkit/Views/MediaElement/Android/MediaElementRenderer.android.cs @@ -21,7 +21,7 @@ public class MediaElementRenderer : FrameLayout, IVisualElementRenderer, IViewRe VisualElementTracker? tracker; protected MediaController? controller; protected MediaPlayer? mediaPlayer; - protected FormsVideoView view; + protected FormsVideoView? view; bool isDisposed; int? defaultLabelFor; @@ -47,9 +47,14 @@ public MediaElementRenderer(Context context) public override float Alpha { - get => view.Alpha; + get => view?.Alpha ?? throw new ObjectDisposedException(typeof(FormsVideoView).FullName); set { + if (view == null) + { + throw new ObjectDisposedException(typeof(FormsVideoView).FullName); + } + // VideoView opens a separate Window above the current one. // This is because it is based on the SurfaceView. // And we cannot set alpha or perform animations with it because it is not synchronized with your other UI elements. @@ -509,6 +514,7 @@ protected virtual void ReleaseControl() view.SetOnPreparedListener(null); view.SetOnCompletionListener(null); view.Dispose(); + view = null; } if (controller != null)