How to get a callback from AVQueuePlayer / AVPlayerItem when the network becomes active (and ready to play)?

I have an AVQueuePlayer based audio stream player that needs to run in the background. The player must continue to work until he finishes playing the entire list. Of course, you will need to pause if the network connection deteriorates, but it should automatically resume when the network connection returns.

For this I am tracking

  • AVPlayerItem.status
  • AVPlayerItem.playbackLikelyToKeepUp
  • AVPlayer.currentItem
  • AVPlayer.status
  • AVPlayer.error

using KVO. For example, if a network connection is restored from a temporary error, I expect currentAVPlayerItem.status become the AVPlayerStatusItemReadyToPlay reported to my player, and I can release [AVQueuePlayer play] at this point.

This mechanism (logic) seems to work in some cases, but quite often in offline mode, AVPlayerItem.status becomes AVPlayerItemStatusFailed , which the AVQueuePlayer seems to skip automatically. As a result, the queue will be empty and feedback cannot be received from AVPlayerItem .

I was hoping that some property in AVPlayer will change when the network connection deteriorates / restores, but nothing changes ( status , error , etc.).

Obviously, I can track Reachability , but this does not work when the application is running in the background.

How do I know when the network is back and AVQueuePlayer is playing again in this situation?

I may just be a beginner in this area. If someone can give me a general idea of ​​how he / she achieves a continuous experience playing AVQueuePlayer without being completely stopped by temporary network connectivity issues, this will be a huge help too!

+8
ios iphone audio avplayer avqueueplayer
source share
1 answer

You can track AVPlayerItemPlaybackStalledNotification to find out when an item has finished playing its buffer, but not the entire file:

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(AVPlayerItemPlaybackStalledNotification:) name:AVPlayerItemPlaybackStalledNotification object:playerItem]; 

Once there is enough buffer to continue, you should get a notification sent to playbackLikelyToKeepUp , which looks like you're already tracking.

0
source share

All Articles