检测AVPlayerViewController是在播放视频还是在缓冲,然后将叠加层添加到播放器 [英] Detect if AVPlayerViewController is playing video or buffering and add overlay to the player

查看:144
本文介绍了检测AVPlayerViewController是在播放视频还是在缓冲,然后将叠加层添加到播放器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  1. 我必须检测视频是处于播放还是缓冲模式.我正在从URL加载视频.我已经尝试了以下代码,并且能够在视频开始播放后进行跟踪,但是在处于缓冲状态时无法跟踪.
  2. 我还想在播放器中添加一个叠加视图.我试图在AVPlayer中添加叠加层,但是在全屏模式下,叠加层会消失.

需要一些建议.提前致谢.下面是我的代码:

Need some suggestions. Thanks in advance. Below is my code:

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

在此处检查项目

推荐答案

对于类似的问题,我采取了略有不同的方法-在AVPlayer开始实际播放时更新UI(在我们的示例中是音频播放,但是相同原则).

I took a slightly different approach to a similar problem - updating the UI when the AVPlayer starts actual playback (in our case, it was audio playback, but the same principle would apply).

我们使用边界时间在回放开始时(特别是在回放的1/3秒之后)执行一个块,以在可听回放的同时更新UI:

We used a boundary time to execute a block at the start of playback – specifically after 1/3 of a second of playback – to update the UI at the same time as audible playback:

let times = [NSValue(time:CMTimeMake(1,3))]
_ = self.player.addBoundaryTimeObserver(forTimes: times, queue: DispatchQueue.main, using: {
    [weak self] time in
    // Code to update the UI goes here, e.g.
    self?.someUIView.isHidden = false
})

我在我们公司的博客上详细介绍了该方法(以及Xcode示例项目): http://www.artermobilize.com/blog/2017/02/09/detect-when-ios-avplayer-finishes-buffering -using-swift/

I have a little more detail on the approach (and a sample Xcode project) on our company blog: http://www.artermobilize.com/blog/2017/02/09/detect-when-ios-avplayer-finishes-buffering-using-swift/

关于问题2,我同意Marco提出的在AVPlayer层上使用UIView的建议.

As to question #2, I concur with Marco's suggestion of using an UIView overtop of the AVPlayer layer.

这篇关于检测AVPlayerViewController是在播放视频还是在缓冲,然后将叠加层添加到播放器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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