I'm trying to understand how to properly detect when a player item can play again.
See below observer logic:
if (object == playerItem && [keyPath isEqualToString:@"playbackBufferEmpty"])
{
if (playerItem.playbackBufferEmpty)
{
// show loading indicator
}
}
if (object == playerItem && [keyPath isEqualToString:@"playbackLikelyToKeepUp"])
{
if (playerItem.playbackLikelyToKeepUp)
{
// hide loading indicator
if (playerItem.status == AVPlayerItemStatusReadyToPlay) {
// start playing
}
else if (playerItem.status == AVPlayerStatusFailed) {
// handle failed
}
else if (playerItem.status == AVPlayerStatusUnknown) {
// handle unknown
}
}
}
Is checking for AVPlayerItemStatusReadyToPlay
underneath playbackLikelyToKeepUp
overkill?
Or
Should I only listen to status
change on the player item instead of bothering with playbackLikelyToKeepUp
?
The two properties inform us of two different pieces of information in regards to the status of an AVPlayerItem. AVPlayerItemStatusReadyToPlay
is a constant that will indicate readyToPlay
ONLY once an AVPlayer has been given sufficient time to buffer enough of the item's data such that it is able to BEGIN playback of the item. But that is it. Just because an item is ready to play, doesn't mean that it won't stall after the first few seconds.
playBackLikelyToKeepUp
returns a bool
indicating that playback of the item is "likely" to keep up throughout the duration of the item. This property does NOT only pertain to the beginning of the item, like AVPlayerItemStatusReadyToPlay
. It does not "care" if the item is ready for playback, all it "cares" about is wether or not it "thinks" that playback of the item will keep up without stalling. This is a PREDICTION of playability that takes into account various factors which you can read about here -> https://developer.apple.com/documentation/avfoundation/avplayeritemstatus
So in regards to your question, is it overkill to check the value of AVPlayerItemStatusReadyToPlay
after you've already checked playbackLikelyToKeepUp
... well it is up to you. I personally would check both. I would want to ensure first that the item is ready to play, meaning that sufficient data has been buffered by the AVPlayer
in order to begin playback. AND I would then want to ensure that playbackLikeyToKeepUp == true
so that I can have some degree of certainty that the user's media experience won't be interrupted. But if all you care about is knowing when an item is ready to begin playback again, then you only need check the status
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With