1

I have using reachability to determine network change but once I determine I am trying to reload item but it is not working

// Check if the playback could keep up after a network interruption
private func checkNetworkInterruption() {
    guard
        let item = playerItem,
        !item.isPlaybackLikelyToKeepUp,
        reachability?.connection != .unavailable else { return }

    self.player?.pause()

    // Wait 1 sec to recheck and make sure the reload is needed
    DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1) {
        if !item.isPlaybackLikelyToKeepUp {
            self.reloadItem()
        }
        self.isPlaying ? self.playSong() : self.player?.pause()
    }
}

private func reloadItem() { player?.replaceCurrentItem(with: nil) player?.replaceCurrentItem(with: playerItem) }

Kedar Sukerkar
  • 1,410
  • 1
  • 16
  • 22

1 Answers1

0

AVPlayer can handle the network interrupts by itself, when network fails, the video is paused and keep in buffering status when network is stable, the video is resumed.

but if you need to handle the network interruption, you can try:

private func checkNetworkInterruption() {
    guard
        let item = playerItem,
        !item.isPlaybackLikelyToKeepUp,
        reachability?.connection != .unavailable else { return }

    self.player?.pause()

    // Wait 1 sec to recheck and make sure the reload is needed
    DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1) {
        if !item.isPlaybackLikelyToKeepUp {
            //you can create a new AVItem here 
            player?.replaceCurrentItem(with: item)
        }
        self.isPlaying ? self.playSong() : self.player?.pause()
    }
}