gpt4 book ai didi

swift - AVPlayer 暂停 + 等待后无法恢复

转载 作者:搜寻专家 更新时间:2023-10-30 22:16:20 24 4
gpt4 key购买 nike

在 .pause() 之后,如果我调用 .play() 可以继续,但如果我在 .pause() 之后等待 30-60 秒并尝试 .play(),它有时会无法播放,

  • AVPlayerStatus.Failed 返回 false

  • AVPlayerStatus.ReadyToPlay 返回真值

我应该使用 url 重新初始化播放器以使其工作。现在我想这样做,如果可以播放播放器,我只想调用 .play() 但如果不能,我想重新初始化它,我的问题是如何检测播放器是否可以播放?顺便说一下,它是一个扩展名为 .pls 的 radio 链接

最佳答案

确实没有迹象表明玩家在很长一段时间后恢复时处于不确定状态。(除了在我的测试中我看到在这种情况下从 AVPlayerItem 接收到的元是空的)

无论如何..根据我从互联网上收集到的信息(没有关于此的适当文档),当您暂停时,播放器将在后台继续缓冲并且..如果您在 50-60 秒后尝试恢复它只是不能。停止功能在这里会很好。

我的解决方案:一个简单的计时器,用于检查 50 秒是否已过去,如果已过去,则更新标志以了解在调用 resume 方法时我想开始使用新播放器。

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

关于swift - AVPlayer 暂停 + 等待后无法恢复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36617232/

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