How to get a callback from AVQueuePlayer/AVPlayerItem when network become active (and is ready to play)?
Asked Answered
C

1

8

I have a AVQueuePlayer based audio streaming player that needs to run in the background. The player needs to continue running until it finishes playing all the list. Of course it is going to have to pause if the network connection becomes bad, but it should automatically resume when the network connection comes back.

For this, I am monitoring

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

using KVO. For example, if the network connection recovers from temporary error, I expect currentAVPlayerItem.status becomes AVPlayerStatusItemReadyToPlay, which will be reported to my player and I can issue [AVQueuePlayer play] at that point.

This mechanism (logic) seems to work some cases, but quite frequently in offline environment, AVPlayerItem.status becomes AVPlayerItemStatusFailed, which item AVQueuePlayer seems to skip automatically. As a result, the queue will end up being empty and no callback can be received from AVPlayerItem.

I was hoping that some property in AVPlayer will change when network connection goes bad/recovers, but nothing seems to change (status, error etc).

Obviously I can monitor Reachability, but that doesn't work when the app is running in the background.

How can I know when the network comes back up and AVQueuePlayer is playable again in this situation?

I may be just being a novice around this area. If anybody can give me a general idea how he/she achieves continuous AVQueuePlayer playing experience without being completely stopped by temporary network connection issues, that would be an awesome help as well!

Congo answered 21/11, 2012 at 19:30 Comment(1)
Having the same issue. Did you find any solution to that?Klopstock
L
0

You can monitor AVPlayerItemPlaybackStalledNotification to know when the item finished playing its buffer but not the entire file:

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

Once there's enough buffer to continue, you should get a notification sent to playbackLikelyToKeepUp which it sounds like you're already monitoring.

Lieabed answered 2/2, 2015 at 17:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.