diff --git a/Classes/Player/AVPlayerEngine/AVPlayerEngine+Observation.swift b/Classes/Player/AVPlayerEngine/AVPlayerEngine+Observation.swift index c0f59c1a..8fb40a78 100644 --- a/Classes/Player/AVPlayerEngine/AVPlayerEngine+Observation.swift +++ b/Classes/Player/AVPlayerEngine/AVPlayerEngine+Observation.swift @@ -237,21 +237,20 @@ extension AVPlayerEngine { } // In all other cases, the player manages pause in the timebaseChanged event! - //This is only when the user clicks the pause button on the TV and the app needs refreshing UI in BG. - PKLog.verbose("isAppInBackground = \(isAppInBackground)") + //This is only when the user clicks the pause button on the exteranl TV remote control, and the app needs refreshing UI in sender device. + PKLog.debug("isAppInBackground = \(isAppInBackground)") if isAppInBackground{ let isPaused = self.rate == 0 - PKLog.verbose("isPaused = \(isPaused)") + PKLog.debug("isPaused = \(isPaused)") if isPaused{ if self.currentState != .idle && self.currentState != .ended && self.currentState != .error { - PKLog.verbose("player send pause event!!!") + PKLog.debug("player send pause event!!!") self.lastTimebaseRate = 0 self.post(event: PlayerEvent.Pause()) } } - } } diff --git a/Classes/Player/AVPlayerEngine/AVPlayerEngine.swift b/Classes/Player/AVPlayerEngine/AVPlayerEngine.swift index 5b728176..e20a5185 100644 --- a/Classes/Player/AVPlayerEngine/AVPlayerEngine.swift +++ b/Classes/Player/AVPlayerEngine/AVPlayerEngine.swift @@ -55,7 +55,20 @@ public class AVPlayerEngine: AVPlayer { var onEventBlock: ((PKEvent) -> Void)? - var isAppInBackground: Bool = false + var isAppInBackground: Bool{ + switch UIApplication.shared.applicationState { + case .active: + return false + case .inactive: + return true + case .background: + return true + @unknown default: + return false + } + } + + public weak var view: PlayerView? { didSet { view?.player = self @@ -461,8 +474,7 @@ extension AVPlayerEngine: AppStateObservable { guard let self = self else { return } PKLog.debug("player: \(self)\n Did enter background, finishing up...") - self.isAppInBackground = true - + self.startBackgroundTask() if self.allowAudioFromVideoAssetInBackground { @@ -473,14 +485,28 @@ extension AVPlayerEngine: AppStateObservable { guard let self = self else { return } PKLog.debug("player: \(self)\n Will enter foreground...") - self.isAppInBackground = false - + self.endBackgroundTask() if self.playerLayer?.player == nil { self.playerLayer?.player = self } + }), + + NotificationObservation(name: UIApplication.willResignActiveNotification, onObserve: { + [weak self] in + guard let self = self else { return } + PKLog.debug("player: \(self)\n app is no longer active and loses focus...") + + }), + + NotificationObservation(name: UIApplication.didBecomeActiveNotification, onObserve: { + [weak self] in + guard let self = self else { return } + PKLog.debug("player: \(self)\n app becomes active (fcused)...") + }) + ] }