>我必须检测视频是处于播放还是缓冲模式.我正在从URL加载视频.我已经尝试了下面的代码,我可以在视频开始播放后跟踪,但不能在它处于缓冲状态时跟踪.
>另外,我想在播放器中添加叠加视图.我试图在AVPlayer中添加叠加层,但在全屏模式下叠加层消失.
需要一些建议.提前致谢.以下是我的代码:
let playerAV = AVPlayerViewController()
var player = AVPlayer()
player = AVPlayer(URL: url)
print(url)
playerAV.player = player
playerAV.view.frame = CGRectMake(0, 0, self.videoView.frame.width, self.videoView.frame.height)
self.addChildViewController(playerAV)
self.videoView.addSubview(playerAV.view)
playerAV.didMoveToParentViewController(self)
playerAV.player?.play()
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ChannelDetailViewController.notificationObserver(_:)), name:AVPlayerItemDidPlayToEndTimeNotification , object: player.currentItem)
_ = UIDevice.beginGeneratingDeviceOrientationNotifications
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ChannelDetailViewController.deviceOrientationDidChange(_:)) , name:
UIDeviceOrientationDidChangeNotification, object: nil)
player.addObserver(self, forKeyPath: "rate", options: NSKeyValueObservingOptions.New, context: nil)
player.addPeriodicTimeObserverForInterval(CMTime(value: 1, timescale: 3), queue: dispatch_get_main_queue()) { [weak self] time in
self?.handlePlayerStatus(time)
}
func handlePlayerStatus(time: CMTime) {
if player.status == .ReadyToPlay {
// buffering is finished, the player is ready to play
print("playing")
}
if player.status == .Unknown{
print("Buffering")
}
}
override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
if keyPath == "rate" {
if let rate = change?[NSKeyValueChangeNewKey] as? Float {
if player.currentItem!.status == AVPlayerItemStatus.ReadyToPlay{
if rate != 0 && player.error == nil {
print("normal playback")
}
else{
print("playback stopped")
}
}else if player.currentItem?.status == AVPlayerItemStatus.Unknown{
print("test")
}
}
}
print("you are here")
}
最佳答案 >检查我的答案:
https://stackoverflow.com/a/38867386/5109911,这将告诉你如何检查播放器是否正在加载缓冲区,检查它是否已准备好播放你必须同时检查player.currentItem.status和player.status
>要向AVPlayer添加叠加层,我建议在AVPlayer图层上方使用UIView.