Skip to content

Commit

Permalink
final fix (using computer property)
Browse files Browse the repository at this point in the history
  • Loading branch information
imberezin committed Oct 8, 2024
1 parent 0c3def6 commit 873e17a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}

}
}

Expand Down
36 changes: 31 additions & 5 deletions Classes/Player/AVPlayerEngine/AVPlayerEngine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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)...")

})

]
}

Expand Down

0 comments on commit 873e17a

Please sign in to comment.