暂停+等待一段时间后AVPlayer无法恢复

tur*_*han 6 avplayer swift swift2

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

  • AVPlayerStatus.Failed返回false

  • AVPlayerStatus.ReadyToPlay返回true

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

Mug*_*rel 1

事实上,没有任何迹象表明,在很长一段时间后恢复时,玩家会陷入困境。(除了在我的测试中我看到在这种情况下从 AVPlayerItem 收到的元为空)

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

我的解决方案:一个简单的计时器来检查 50 秒是否过去了,如果是,则更新一个标志以了解当调用恢复方法时我想开始一个新的玩家。

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()
        ...
    }
    ...
}
Run Code Online (Sandbox Code Playgroud)