[英]Check play state of AVPlayer and add overlay view to AVPlayerViewController
我想檢查AVPlayerViewController是否正在播放視頻或其仍在緩沖中。我還想在此AVPlayerViewController中使用下一個和上一個按鈕添加疊加視圖。 使用以下代碼,我的視頻正在緩沖中,但顯示正常播放。我想跟蹤其播放,暫停還是緩沖階段。
let playerAV = AVPlayerViewController()
var player = AVPlayer()
let videoURL = NSURL(string: "https://clips.vorwaerts-gmbh.d e/big_buck_bunny.mp4")
player = AVPlayer(URL:videoURL!)
playerAV.player = player
playerAV.view.frame = self.movieView.frame
self.addChildViewController(playerAV)
self.movieView.addSubview(playerAV.view)
playerAV.didMoveToParentViewController(self)
playerAV.contentOverlayView?.addSubview(viewNext)
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ViewController.notificationObserver(_:)), name:AVPlayerItemDidPlayToEndTimeNotification , object: player.currentItem)
player.addObserver(self, forKeyPath: "rate", options: NSKeyValueObservingOptions.New, context: nil)
_ = UIDevice.beginGeneratingDeviceOrientationNotifications
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ViewController.deviceOrientationDidChange(_:)) , name:
UIDeviceOrientationDidChangeNotification, object: nil)
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 rate == 0.0 {
print("playback stopped")
}
if rate == 1.0 {
print("normal playback")
}
if rate == -1.0 {
print("reverse playback")
}
}
}
print("you are here")
}
為了檢查播放器的狀態,您可以添加一個定期的時間觀察者:
player.addPeriodicTimeObserverForInterval(CMTime(value: 1, timescale: 3), queue: dispatch_get_main_queue()) { [weak self] time in
self?.handlePlayerStatus(time)
}
在handlePlayerStatus
內部,您檢查狀態:
func handlePlayerStatus(time: CMTime) {
if player.status == .ReadyToPlay {
// buffering is finished, the player is ready to play
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.