From 05f8dd04dcc0bc683e662797ddd111b3d2416469 Mon Sep 17 00:00:00 2001 From: Jammer Date: Tue, 8 Jun 2021 16:50:39 +0100 Subject: [PATCH] Fixes #629 and Tidied up the code from Jearom. --- .../Ios/Video/PlayerViewController.cs | 44 ++++++++++++++++++- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/MediaManager/Platforms/Ios/Video/PlayerViewController.cs b/MediaManager/Platforms/Ios/Video/PlayerViewController.cs index 5f42a0dd..39b729fe 100644 --- a/MediaManager/Platforms/Ios/Video/PlayerViewController.cs +++ b/MediaManager/Platforms/Ios/Video/PlayerViewController.cs @@ -1,11 +1,24 @@ -using AVKit; +using System; +using AVKit; +using CoreGraphics; +using Foundation; +using UIKit; namespace MediaManager.Platforms.Ios.Video { public class PlayerViewController : AVPlayerViewController { + private const string KeyPath = "contentOverlayView.frame"; + + private bool _savePlayer; + protected static MediaManagerImplementation MediaManager => CrossMediaManager.Apple; + public PlayerViewController() + { + AddObserver(this, KeyPath, NSKeyValueObservingOptions.OldNew, Handle); + } + public override void ViewWillAppear(bool animated) { base.ViewWillAppear(animated); @@ -23,7 +36,34 @@ public override void ViewWillDisappear(bool animated) MediaManager.MediaPlayer.VideoView = null; } - Player = null; + if (!_savePlayer) + { + Player = null; + } + } + + public override void ObserveValue(NSString keyPath, NSObject ofObject, NSDictionary change, IntPtr context) + { + if (keyPath != KeyPath || change == null || change[ChangeOldKey] == null) return; + + var oldNsValue = change[ChangeOldKey]; + var newNsValue = change[ChangeNewKey]; + + if (oldNsValue == null || newNsValue == null) return; + + var oldValue = ((NSValue) oldNsValue).CGRectValue; + var newValue = ((NSValue) newNsValue).CGRectValue; + + if (oldValue == CGRect.Empty || oldValue == CGRect.Null || oldValue.Equals(newValue)) return; + + if (UIScreen.MainScreen.Bounds.Height > UIScreen.MainScreen.Bounds.Width) + { + _savePlayer = newValue.Y >= oldValue.Y; + } + else + { + _savePlayer = newValue.Height >= UIScreen.MainScreen.Bounds.Height; + } } } }