Skip to content

Commit

Permalink
fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
imberezin committed Oct 1, 2024
1 parent 258c67b commit 0c3def6
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions Classes/Player/AVPlayerEngine/AVPlayerEngine+Observation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -205,13 +205,18 @@ extension AVPlayerEngine {
@objc func timebaseChanged(notification: Notification) {
// For some reason timebase rate changed is received on a background thread.
// in order to check self.rate we must make sure we are on the main thread.

DispatchQueue.main.async {
guard let timebase = self.currentItem?.timebase else { return }
PKLog.verbose("timebase changed, current timebase: \(String(describing: timebase))")
let timebaseRate = CMTimebaseGetRate(timebase)
if timebaseRate > 0 && self.lastTimebaseRate != timebaseRate {
PKLog.verbose("timebaseChanged->PlayerEvent.Playing")

self.post(event: PlayerEvent.Playing())
} else if timebaseRate == 0 && self.rate == 0 && self.lastTimebaseRate != timebaseRate {
PKLog.verbose("timebaseChanged->PlayerEvent.Pause")

self.post(event: PlayerEvent.Pause())
}
// Make sure to save the last value so we could only post events only when currentTimebase != lastTimebase
Expand All @@ -223,7 +228,7 @@ extension AVPlayerEngine {
*/
/// Handles changes in player rate
private func handleRate() {
PKLog.debug("player rate was changed, now: \(self.rate)")
PKLog.verbose("player rate was changed, now: \(self.rate)")

if let isPlaybackLikelyToKeepUp = self.currentItem?.isPlaybackLikelyToKeepUp, isPlaybackLikelyToKeepUp == false {
if self.rate == 0, self.currentState == .buffering || self.currentState == .ready {
Expand All @@ -233,10 +238,15 @@ 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)")
if isAppInBackground{

let isPaused = self.rate == 0
if isPaused && self.lastTimebaseRate > 0{
PKLog.verbose("isPaused = \(isPaused)")

if isPaused{
if self.currentState != .idle && self.currentState != .ended && self.currentState != .error {
PKLog.verbose("player send pause event!!!")
self.lastTimebaseRate = 0
self.post(event: PlayerEvent.Pause())
}
Expand Down

0 comments on commit 0c3def6

Please sign in to comment.