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

Commit

Permalink
Remove and dispose observer (#392)
Browse files Browse the repository at this point in the history
* Remove and dispose observer
Catch exception when get position.

* Fix crash when App goto background

* reset get position changes

* Update XamarinCommunityToolkit/Views/MediaElement/iOS/MediaElementRenderer.ios.cs

* Update XamarinCommunityToolkit/Views/MediaElement/iOS/MediaElementRenderer.ios.cs

Co-authored-by: Andrei <andrei.misiukevich@gmail.com>
Co-authored-by: Gerald Versluis <gerald.versluis@microsoft.com>
  • Loading branch information
3 people committed Oct 19, 2020
1 parent 72f48e6 commit 1efd0d4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 24 deletions.
Expand Up @@ -133,16 +133,9 @@ protected string ResolveMsAppDataUri(Uri uri)
protected void RemoveStatusObserver()
{
if (statusObserver != null)
{
try
{
avPlayerViewController?.Player?.CurrentItem?.RemoveObserver(statusObserver, "status");
}
finally
{
statusObserver = null;
}
}
avPlayerViewController?.Player?.CurrentItem?.RemoveObserver(statusObserver, "status");
statusObserver?.Dispose();
statusObserver = null;
}

protected virtual void ObserveRate(NSObservedChange e)
Expand Down Expand Up @@ -202,7 +195,7 @@ TimeSpan Position
{
get
{
if (avPlayerViewController.Player?.CurrentTime.IsInvalid ?? true)
if (avPlayerViewController?.Player?.CurrentTime.IsInvalid ?? true)
return TimeSpan.Zero;

return TimeSpan.FromSeconds(avPlayerViewController.Player.CurrentTime.Seconds);
Expand Down Expand Up @@ -377,19 +370,7 @@ protected override void OnElementChanged(ElementChangedEventArgs<MediaElement> e
e.OldElement.SeekRequested -= MediaElementSeekRequested;
e.OldElement.StateRequested -= MediaElementStateRequested;
e.OldElement.PositionRequested -= MediaElementPositionRequested;

if (playedToEndObserver != null)
{
NSNotificationCenter.DefaultCenter.RemoveObserver(playedToEndObserver);
playedToEndObserver = null;
}

SetKeepScreenOn(false);
RemoveStatusObserver();
rateObserver?.Dispose();
rateObserver = null;
volumeObserver?.Dispose();
volumeObserver = null;

// stop video if playing
if (avPlayerViewController?.Player?.CurrentItem != null)
Expand All @@ -401,6 +382,23 @@ protected override void OnElementChanged(ElementChangedEventArgs<MediaElement> e
avPlayerViewController?.Player?.ReplaceCurrentItemWithPlayerItem(null);
AVAudioSession.SharedInstance().SetActive(false);
}

if (playedToEndObserver != null)
NSNotificationCenter.DefaultCenter.RemoveObserver(playedToEndObserver);
playedToEndObserver?.Dispose();
playedToEndObserver = null;

if (rateObserver != null)
avPlayerViewController?.Player?.RemoveObserver(rateObserver, "rate");
rateObserver?.Dispose();
rateObserver = null;

if (volumeObserver != null)
avPlayerViewController?.Player?.RemoveObserver(volumeObserver, "volume");
volumeObserver?.Dispose();
volumeObserver = null;

RemoveStatusObserver();
}

if (e.NewElement != null)
Expand All @@ -426,4 +424,4 @@ protected override void OnElementChanged(ElementChangedEventArgs<MediaElement> e

protected virtual void UpdateBackgroundColor() => BackgroundColor = Element.BackgroundColor.ToUIColor();
}
}
}
4 changes: 4 additions & 0 deletions XamarinCommunityToolkitSample.iOS/Info.plist
Expand Up @@ -42,5 +42,9 @@
<string>This app would like to save files to your gallery</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>This app would like to access your gallery</string>
<key>UIBackgroundModes</key>
<array>
<string>audio</string>
</array>
</dict>
</plist>

0 comments on commit 1efd0d4

Please sign in to comment.