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

Commit

Permalink
Implemented MediaElement ObjectDisposedException fix (#1870)
Browse files Browse the repository at this point in the history
Implemented fix
  • Loading branch information
anvuks committed Jun 29, 2022
1 parent 22b1d4c commit 12f9d71
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,14 @@ protected void ExtractMetadata(MediaMetadataRetriever retriever)

public override async void SetVideoURI(global::Android.Net.Uri? uri, IDictionary<string, string>? 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<string, string>? headers)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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.
Expand Down Expand Up @@ -509,6 +514,7 @@ protected virtual void ReleaseControl()
view.SetOnPreparedListener(null);
view.SetOnCompletionListener(null);
view.Dispose();
view = null;
}

if (controller != null)
Expand Down

2 comments on commit 12f9d71

@H7mosh
Copy link

@H7mosh H7mosh commented on 12f9d71 Feb 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How Can I Navigate To Those Methods In My Project ???

@H7mosh
Copy link

@H7mosh H7mosh commented on 12f9d71 Feb 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image
I'm Using This Version Of Xamarin

Please sign in to comment.