Skip to content

Commit

Permalink
Fix error notifcation name (#482)
Browse files Browse the repository at this point in the history
* fix player notification names
update version to 3.30.2

* fix bug:
when the app uses airplay, and the user pauses the player via TV, the pause event doesn't send.
  • Loading branch information
imberezin authored Oct 8, 2024
1 parent 3d0061a commit 5b64185
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 4 deletions.
28 changes: 26 additions & 2 deletions Classes/Player/AVPlayerEngine/AVPlayerEngine+Observation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -205,29 +205,53 @@ 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
self.lastTimebaseRate = timebaseRate
}
}

/* When setting automaticallyWaitsToMinimizeStalling and shouldPlayImmediately, the player may be stalled and the rate will be changed to 0, player paused, by the AVPlayer. Therefor we are sending a paused event.
*/
/// Handles changes in player rate
private func handleRate() {
PKLog.debug("player rate was changed, now: \(self.rate)")
// When setting automaticallyWaitsToMinimizeStalling and shouldPlayImmediately, the player may be stalled and the rate will be changed to 0, player paused, by the AVPlayer. Therefor we are sending a paused event.
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 {
self.post(event: PlayerEvent.Pause())
}
}

// In all other cases, the player manages pause in the timebaseChanged event!
//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.debug("isPaused = \(isPaused)")

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

/// Handle changes in Audio/Video tracks codecs.
Expand Down
33 changes: 32 additions & 1 deletion Classes/Player/AVPlayerEngine/AVPlayerEngine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,20 @@ public class AVPlayerEngine: AVPlayer {

var onEventBlock: ((PKEvent) -> Void)?

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 @@ -458,8 +472,9 @@ extension AVPlayerEngine: AppStateObservable {
}),
NotificationObservation(name: UIApplication.didEnterBackgroundNotification, onObserve: { [weak self] in
guard let self = self else { return }

PKLog.debug("player: \(self)\n Did enter background, finishing up...")

self.startBackgroundTask()

if self.allowAudioFromVideoAssetInBackground {
Expand All @@ -470,12 +485,28 @@ extension AVPlayerEngine: AppStateObservable {
guard let self = self else { return }

PKLog.debug("player: \(self)\n Will enter foreground...")

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
2 changes: 1 addition & 1 deletion PlayKit.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ suffix = '.0000' # Dev mode
Pod::Spec.new do |s|

s.name = 'PlayKit'
s.version = '3.30.2' + suffix
s.version = '3.30.3' + suffix
s.summary = 'PlayKit: Kaltura Mobile Player SDK - iOS'
s.homepage = 'https://github.com/kaltura/playkit-ios'
s.license = { :type => 'AGPLv3', :text => 'AGPLv3' }
Expand Down

0 comments on commit 5b64185

Please sign in to comment.