func pausePlayer() {
..
player.pause()
..
// Will count to default 50 seconds or the indicated interval and only then set the bufferedInExcess flag to true
startCountingPlayerBufferingSeconds()
bufferedInExcess = false
}
func startCountingPlayerBufferingSeconds(interval: Double = 50) {
timer = NSTimer.scheduledTimerWithTimeInterval(interval, target: self, selector: Selector("setExcessiveBufferedFlag"), userInfo: nil, repeats: false)
}
func setExcessiveBufferedFlag() {
if DEBUG_LOG {
print("Maximum player buffering interval reached.")
}
bufferedInExcess = true
}
func stopCountingPlayerBufferingSeconds() {
timer.invalidate()
}
func resumePlayer() {
if haveConnectivity() {
if (.. || bufferedInExcess) {
startPlaying(true)
} else {
..
player.play
}
..
}
}
func startPlaying(withNewPlayer: Bool = false) {
if (withNewPlayer) {
if DEBUG_LOG {
print("Starting to play on a fresh new player")
}
// If we need another player is very important to fist remove any observers for
// the current AVPlayer/AVPlayerItem before reinitializing them and add again the needed observers
initPlayer()
player.play()
...
}
...
}
let status = player.timeControlStatus
switch status {
case .paused:
print("timeControlStatus.paused")
case .playing:
print("timeControlStatus.playing")
case .waitingToPlayAtSpecifiedRate:
print("timeControlStatus.waitingToPlayAtSpecifiedRate")
if let reason = player.reasonForWaitingToPlay {
switch reason {
case .evaluatingBufferingRate:
print("reasonForWaitingToPlay.evaluatingBufferingRate")
case .toMinimizeStalls:
print("reasonForWaitingToPlay.toMinimizeStalls")
case .noItemToPlay:
print("reasonForWaitingToPlay.noItemToPlay")
default:
print("Unknown \(reason)")
}
}
}
3条答案
按热度按时间pcrecxhr1#
实际上,没有任何迹象表明,当恢复后很长一段时间的播放器是在地狱。(除了在我的测试中,我看到,在这种情况下,从AVPlayerItem收到的 meta是空的)
无论如何..从我从互联网上收集的(没有适当的文档),当你暂停,播放器将继续在后台缓冲..如果你试图恢复后50-60秒,它只是不能。停止功能将是很好的。
我的解决方案:一个简单的计时器,用于检查50秒是否已过,如果已过,则更新一个标志,以了解当调用resume方法时,我希望开始一个新的播放器。
mznpcxlj2#
假设您在发出.play()之前已经检查了AVPlayerItem.status == .playable
然后使用AVPlayer示例的timeControlStatus,该示例“指示播放当前是否正在进行、是否无限期暂停或是否在等待适当的网络条件时暂停”。
在我的例子中,它在waitingToPlayAtSpecifiedRate.evaluatingBufferingRate模式下卡住了。此时,AVPlayerItem.status示例是readToPlay。
在写这篇文章的时候,每当收到startCommand时,我都会重置AVplayer以确保万无一失。但这似乎很笨重。正在寻找一个更流畅的解决方案。
xurqigkl3#
我在目标c上发布我的解决方案。所以我在按下暂停按钮时添加了一个计时器,如果该计时器在85秒后触发(在这些秒后缓冲停止),那么我将布尔值设置为YES,当恢复音频时,如果该布尔值为YES,那么我创建一个新的播放器。