(注:我在运行iOS 9和正在做的HTTP实时流)
我有一个AVPlayer
并想显示加载指示灯在播放器缓冲。当玩家开始以缓慢的连接志愿被调用这些属性玩:isPlaybackBufferEmpty
isPlaybackLikelyToKeepUp
isPlaybackBufferFull
问题是,这些相同的属性没有得到时再缓冲完成所谓的(当我说做我的意思是,视频是确定再玩一次。)我的目标是隐藏在正确的时间负荷指标,但这些不再次调用。
我在网上搜索,发现这个雷达:http://www.openradar.me/25931165如果不知道它的100%相关
有什么想法吗?
// MARK: - Key-Value Observing Method
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if keyPath == #keyPath(AVPlayerItem.status) {
switch playerItem.status {
case .unknown:
break
case .readyToPlay:
player.play()
case .failed:
postPlaybackDidFailWithErrorNotification(error: error)
}
}
else if keyPath == #keyPath(AVPlayer.currentItem.isPlaybackBufferEmpty) {
guard let currentItem = player.currentItem else {
return
}
if currentItem.isPlaybackBufferEmpty {
print("isPlaybackBufferEmpty = YES")
} else {
print("isPlaybackBufferEmpty = NO")
}
}
else if keyPath == #keyPath(AVPlayer.currentItem.isPlaybackLikelyToKeepUp) {
guard let currentItem = player.currentItem else {
return
}
if currentItem.isPlaybackLikelyToKeepUp {
print("isPlaybackLikelyToKeepUp = YES")
//player.play()
} else {
print("isPlaybackLikelyToKeepUp = NO")
}
}
else if keyPath == #keyPath(AVPlayer.currentItem.isPlaybackBufferFull) {
guard let currentItem = player.currentItem else {
return
}
if currentItem.isPlaybackBufferFull {
print("isPlaybackBufferFull = YES")
//player.play()
} else {
print("isPlaybackBufferFull = NO")
}
}
else if keyPath == #keyPath(AVPlayer.currentItem) {
// Cleanup if needed.
if player.currentItem == nil {
video = nil
playerItem = nil
}
}
else if keyPath == #keyPath(AVPlayer.rate) {
updateMetadata()
NotificationCenter.default.post(name: AssetPlaybackManager.NotificationName.playerRateDidChangeNotification, object: nil)
}
else {
super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
}
}
您可以使用timeControlStatus但它仅适用以上的iOS 10。
根据苹果的官方文档
指示回放当前是否处于进度的状态,无限期地暂停,或悬浮在等待适当的网络条件
这个观察者添加到播放器。
player.addObserver(self, forKeyPath: “timeControlStatus”, options: [.old, .new], context: nil)
然后,观察在变化
func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?)
method.Use下面内部上面的方法的代码
override public func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if keyPath == "timeControlStatus", let change = change, let newValue = change[NSKeyValueChangeKey.newKey] as? Int, let oldValue = change[NSKeyValueChangeKey.oldKey] as? Int {
let oldStatus = AVPlayer.TimeControlStatus(rawValue: oldValue)
let newStatus = AVPlayer.TimeControlStatus(rawValue: newValue)
if newStatus != oldStatus {
DispatchQueue.main.async {[weak self] in
if newStatus == .playing || newStatus == .paused {
self?.loaderView.isHidden = true
} else {
self?.loaderView.isHidden = false
}
}
}
}
}
这是通过迅速4在iOS 11测试上述和其工作正常。
使用下面的函数用于设置AVPlayer进度以及缓存的状态:
func addObserver() {
let intervel : CMTime = CMTimeMake(value: 10, timescale: 10)
observer = player?.addPeriodicTimeObserver(forInterval: intervel, queue: DispatchQueue.main) { [weak self] time in
guard let `self` = self,
let playerItem = self.player?.currentItem else { return }
let currentTime : Float64 = CMTimeGetSeconds(time)
let totalDuration = CMTimeGetSeconds(playerItem.asset.duration)
//this is the slider value update if you are using UISlider.
let sliderValue = (currentTime/totalDuration)
if currentTime >= totalDuration {
if let observer = self.observer{
//removing time observer
self.player?.removeTimeObserver(observer)
self.observer = nil
}
}
let playbackLikelyToKeepUp = self.player?.currentItem?.isPlaybackLikelyToKeepUp
if playbackLikelyToKeepUp == false{
print(self.player?.rate)
print("IsBuffering")
self.lock()
} else {
//stop the activity indicator
print("Buffering completed")
self.unlock()
}
}
}