Skip to content
This repository was archived by the owner on May 1, 2024. It is now read-only.

Commit 12f9d71

Browse files
authored
Implemented MediaElement ObjectDisposedException fix (#1870)
Implemented fix
1 parent 22b1d4c commit 12f9d71

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

src/CommunityToolkit/Xamarin.CommunityToolkit/Views/MediaElement/Android/FormsVideoView.android.cs

+6-2
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,14 @@ protected void ExtractMetadata(MediaMetadataRetriever retriever)
5252

5353
public override async void SetVideoURI(global::Android.Net.Uri? uri, IDictionary<string, string>? headers)
5454
{
55+
base.SetVideoURI(uri, headers);
56+
57+
// this instance could get disposed during awaiting, so a call to the base method (AFTER awaiting)
58+
// would throw ObjectDisposedException and be impossible to catch due to async void
5559
if (uri != null)
60+
{
5661
await SetMetadata(uri, headers);
57-
58-
base.SetVideoURI(uri, headers);
62+
}
5963
}
6064

6165
protected async Task SetMetadata(global::Android.Net.Uri uri, IDictionary<string, string>? headers)

src/CommunityToolkit/Xamarin.CommunityToolkit/Views/MediaElement/Android/MediaElementRenderer.android.cs

+8-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class MediaElementRenderer : FrameLayout, IVisualElementRenderer, IViewRe
2121
VisualElementTracker? tracker;
2222
protected MediaController? controller;
2323
protected MediaPlayer? mediaPlayer;
24-
protected FormsVideoView view;
24+
protected FormsVideoView? view;
2525
bool isDisposed;
2626
int? defaultLabelFor;
2727

@@ -47,9 +47,14 @@ public MediaElementRenderer(Context context)
4747

4848
public override float Alpha
4949
{
50-
get => view.Alpha;
50+
get => view?.Alpha ?? throw new ObjectDisposedException(typeof(FormsVideoView).FullName);
5151
set
5252
{
53+
if (view == null)
54+
{
55+
throw new ObjectDisposedException(typeof(FormsVideoView).FullName);
56+
}
57+
5358
// VideoView opens a separate Window above the current one.
5459
// This is because it is based on the SurfaceView.
5560
// 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()
509514
view.SetOnPreparedListener(null);
510515
view.SetOnCompletionListener(null);
511516
view.Dispose();
517+
view = null;
512518
}
513519

514520
if (controller != null)

0 commit comments

Comments
 (0)