我使用AVPlayer通过互联网播放音频直播。如果暂停时间超过1分钟,我想恢复播放。
我致电player.rate = 1.0
继续。但是,如果流暂停> 1分钟,则不再播放。在这种情况下,我需要重新创建AVPlayerItem以使其再次工作。
所以我怎么能抓住这个案子,所以我知道播放没有恢复?
player.rate
上尝试了KVO。它保持在1.0
。玩家没有玩!currentItem.playbackBufferEmpty
上尝试了KVO。但是在这种情况下不会调用它。currentItem.status
未切换为.Failed
。它完全没有变化。在这种情况下,AVPlayer似乎什么都不做。有什么想法吗?
我构建了一个Playground代码来演示这个问题:
import UIKit
import AVFoundation
// keep it running forever so it plays audio
import XCPlayground
XCPSetExecutionShouldContinueIndefinitely(true)
class AVPlayerTest {
let player = AVPlayer()
let streamurl = NSURL(string: "http://detektor.fm/stream/mp3/musik/")!
func startTest() {
let item = AVPlayerItem(URL: streamurl)
player.replaceCurrentItemWithPlayerItem(item)
player.play()
// give it some start time to build a buffer
NSTimer.scheduledTimerWithTimeInterval(2, target: self, selector: #selector(timerTickedToPause), userInfo: nil, repeats: false)
}
@objc func timerTickedToPause(timer: NSTimer) {
player.pause()
// pause now for some time. 90s is not enough.
NSTimer.scheduledTimerWithTimeInterval(120, target: self, selector: #selector(timerTickedToPlay), userInfo: nil, repeats: false)
}
@objc func timerTickedToPlay(timer: NSTimer) {
// try to resume playback
print(player.rate)
player.play()
print(player.rate)
// check in some seconds if it recovered
NSTimer.scheduledTimerWithTimeInterval(10, target: self, selector: #selector(timerTickedCheck), userInfo: nil, repeats: false)
}
@objc func timerTickedCheck(timer: NSTimer) {
// it reports rate = 1.0 but is not playing here though!
// there is no way to know for me it did not recover here!?
print(player.rate)
// recover by creating a new item works
let item = AVPlayerItem(URL: streamurl)
player.replaceCurrentItemWithPlayerItem(item)
player.play()
}
}
let test = AVPlayerTest()
test.startTest()
答案 0 :(得分:2)
使用AVPlayer实例的 timeControlStatus “指示播放当前是否正在进行,无限期暂停,或在等待适当的网络条件时暂停。”
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)")
}
}
}
我有同样的问题但在我的情况下,如果我在暂停状态下进入后台,则会发生这种情况。当我回到forground .play()不起作用。它会遇到waitingToPlayAtSpecifiedRate.evaluatingBufferingRate模式。 此时,AVPlayerItem.status实例是readToPlay。
在收到startCommand时的写入时间我重置AV播放器以确定。但这似乎很笨拙。寻找更顺畅的解决方案。