(NOTE: I am running iOS 9 and am doing HTTP Live Streaming)
I have an AVPlayer
and want to show a loading indicator while the player is buffering. When the player starts playing with a slow connection KVO gets called for these properties:
isPlaybackBufferEmpty
isPlaybackLikelyToKeepUp
isPlaybackBufferFull
The problem is that these same properties do not get called again when buffering is done (when I say done I mean that the video is ok to play again.) My goal is to hide the loading indicator at the correct time but these don't get called again.
I searched online and found this radar: http://www.openradar.me/25931165 Not sure if it's 100% related
Any thoughts?
// MARK: - Key-Value Observing Method
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if keyPath == #keyPath(AVPlayerItem.status) {
switch playerItem.status {
case .unknown:
break
case .readyToPlay:
player.play()
case .failed:
postPlaybackDidFailWithErrorNotification(error: error)
}
}
else if keyPath == #keyPath(AVPlayer.currentItem.isPlaybackBufferEmpty) {
guard let currentItem = player.currentItem else {
return
}
if currentItem.isPlaybackBufferEmpty {
print("isPlaybackBufferEmpty = YES")
} else {
print("isPlaybackBufferEmpty = NO")
}
}
else if keyPath == #keyPath(AVPlayer.currentItem.isPlaybackLikelyToKeepUp) {
guard let currentItem = player.currentItem else {
return
}
if currentItem.isPlaybackLikelyToKeepUp {
print("isPlaybackLikelyToKeepUp = YES")
//player.play()
} else {
print("isPlaybackLikelyToKeepUp = NO")
}
}
else if keyPath == #keyPath(AVPlayer.currentItem.isPlaybackBufferFull) {
guard let currentItem = player.currentItem else {
return
}
if currentItem.isPlaybackBufferFull {
print("isPlaybackBufferFull = YES")
//player.play()
} else {
print("isPlaybackBufferFull = NO")
}
}
else if keyPath == #keyPath(AVPlayer.currentItem) {
// Cleanup if needed.
if player.currentItem == nil {
video = nil
playerItem = nil
}
}
else if keyPath == #keyPath(AVPlayer.rate) {
updateMetadata()
NotificationCenter.default.post(name: AssetPlaybackManager.NotificationName.playerRateDidChangeNotification, object: nil)
}
else {
super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
}
}