gpt4 book ai didi

ios - AVPlayer中如何处理网络中断

转载 作者:行者123 更新时间:2023-11-29 05:28:53 25 4
gpt4 key购买 nike

我使用可达性来确定网络更改,但一旦我确定我正在尝试重新加载项目,但它不起作用

// 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() {
玩家?.replaceCurrentItem(with: nil)
玩家?.replaceCurrentItem(与:playerItem)
}

最佳答案

AVPlayer可以自行处理网络中断,当网络故障时,视频暂停,保持缓冲状态,当网络稳定时,视频恢复。

但如果需要处理网络中断,可以尝试:

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()
}
}

关于ios - AVPlayer中如何处理网络中断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57870382/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com