暂停+等待一段时间后AVPlayer无法恢复 [英] AVPlayer can't resume after paused + some waiting

查看:216
本文介绍了暂停+等待一段时间后AVPlayer无法恢复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

After .pause() if I call .play() ok it continues but if I wait 30-60 sec after .pause() and try to .play() it sometimes fail to play,

  • AVPlayerStatus.Failed返回false

  • AVPlayerStatus.Failed returns false

AVPlayerStatus.ReadyToPlay返回true

AVPlayerStatus.ReadyToPlay returns true

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

I should re-initialize player with url to make it work. Now I want to do that, if player can be played, I want to just call .play() but if not, I want to re-initialize it, my question is how to detect if player is playable? By the way it is a radio link with .pls extension

推荐答案

假定您在发布.play()之前检查了AVPlayerItem.status == .playable

Assuming you have checked the AVPlayerItem.status == .playable before issuing .play()

然后使用AVPlayer实例的 timeControlStatus 来指示当前正在播放,无限期暂停还是在等待适当的网络条件时暂停播放".

Then use the timeControlStatus of the AVPlayer instance which "indicates whether playback is currently in progress, paused indefinitely, or suspended while waiting for appropriate network conditions."

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

}

在我的情况下,它卡在waitingToPlayAtSpecifiedRate.evaluatingBufferingRate模式下. 那时,AVPlayerItem.status实例为readToPlay.

In my case it gets stuck the waitingToPlayAtSpecifiedRate.evaluatingBufferingRate mode. At that point the AVPlayerItem.status instance is readToPlay.

在撰写本文时,每当收到startCommand时,我都会将AVplayer重置以确保.但这似乎很笨拙.寻找更流畅的解决方案.

At this time of writing whenever a startCommand is received I reset the AVplayer to be sure. But this seems clunky. Looking for a smoother solution.

这篇关于暂停+等待一段时间后AVPlayer无法恢复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆