You should add observer for AVPlayerItemPlaybackStalledNotification.
AVPlayerItemFailedToPlayToEndTimeNotification has no value for me on this problem.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackStalled:) name:AVPlayerItemPlaybackStalledNotification object:trackItem];
As the doc says, file-based playback does not continue if necessary streaming media wasn’t delivered in a timely fashion over a network.
The notification’s object is the AVPlayerItem instance whose playback
was unable to continue because the necessary streaming media wasn’t
delivered in a timely fashion over a network. Playback of streaming
media continues once a sufficient amount of data is delivered.
File-based playback does not continue.
This explained why AVPlayer can resume HLS streams after network switch, but cannot do the same if I use AVPlayer to play TuneIn resources which is file-based.
Then the answer becomes simple.
- (void)playbackStalled:(NSNotification *)notification {
if ([self isFileBased:streamUri]) {
// Restart playback
NSURL *url = [NSURL URLWithString:streamUri];
AVPlayerItem *trackItem = [AVPlayerItem playerItemWithURL:url];
AVPlayer *mediaPlayer = [AVPlayer playerWithPlayerItem:trackItem];
[self registerObservers:trackItem player:mediaPlayer];
[mediaPlayer play];
}
}
Further reading on discussion of automaticallyWaitsToMinimizeStalling.
AVPlayerItemFailedToPlayToEndTimeNotificationas suggested in the other answers. Also it seems that for HLS live video streams, we need to recreate the AVPlayerItem and set it to the AVPlayer. Just callingplay(), doing seeking etc does not seem to work for us. Looking for a better solution right now but this is the best I know at this point. Also if the avplayeritem has.statusfailed, we also need to recreate it (my assumption). - Jonny