Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AVPlayer : How to handle network interruptions

When using AVPlayer to play audio from an url it will discontinue to play when for example disconnecting from wifi.

[player play];

Does not resume the AVPlayer

player.rate // Value is 1.0

player.currentItem.isPlaybackLikelyToKeepUp // Value is YES

player.status // Value is AVPlayerStatusReadyToPlay

player.error // Value is nil

But the player is not playing any audio.

How do I handle a disconnect from AVPlayer, for reconnecting the AVPlayer and start playing again?

like image 428
hallo Avatar asked Oct 17 '13 22:10

hallo


People also ask

How do I stop AVPlayer from buffering?

But you can check the boolean playbackLikelyToKeepUp property of the AVPlayerItem to check if the buffer is loading. I not then pause playback on the AVPlayer. If you will call [self. player replaceCurrentItemWithPlayerItem:nil]; buffering stops.

What is AVPlayer in swift?

An object that provides the interface to control the player's transport behavior.

What is AVPlayer com?

Overview. AVPlayer is a controller object used to manage the playback and timing of a media asset. You can use an AVPlayer to play local and remote file-based media, such as QuickTime movies and MP3 audio files, as well as audiovisual media served using HTTPS Live Streaming.


2 Answers

In order to handle network changes, you have to add an observer for AVPlayerItemFailedToPlayToEndTimeNotification.

- (void) playURL:(NSURL *)url
{
    AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:url];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerItemFailedToPlayToEndTime:) name:AVPlayerItemFailedToPlayToEndTimeNotification object:playerItem];
    self.player = [AVPlayer playerWithPlayerItem:playerItem];
    [self.player play];
}

- (void) playerItemFailedToPlayToEndTime:(NSNotification *)notification
{
    NSError *error = notification.userInfo[AVPlayerItemFailedToPlayToEndTimeErrorKey];
    // Handle error ...
}
like image 87
0xced Avatar answered Sep 23 '22 03:09

0xced


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.

like image 27
Huiyang Shan Avatar answered Sep 22 '22 03:09

Huiyang Shan