ios - 嵌入式 AVPlayerControllerView 添加 AVTouchIgnoringView 阻止默认播放器的界面

标签 ios avkit avplayerviewcontroller

更新:这个问题与 AVPlayerControllerView 无关,请看下面我的回答。类名 AVTouchIgnoringView 一开始让我感到困惑,但这也是思考问题的错误路径。

===

众所周知,Media Player 框架在 iOS 9 中已被弃用,因此我决定在我的新项目中尝试使用 AVKit。我的任务是显示一个嵌入在 Collection View 标题 (UICollectionReusableView) 中的视频播放器,下面有一些任意单元格。

这是我在代码中的做法:

override func viewDidLoad() {
  super.viewDidLoad()

  apiManager.loadVideo() { video in
    let player = AVPlayer(URL: video.url)
    self.playerViewController.view.hidden = true
    self.playerViewController.player = player
    self.addChildViewController(self.playerViewController)

    headerView.videoContainerView.addSubview(self.playerViewController.view)
    Cartography.layout(self.playerViewController.view,
      headerView.videoContainerView) { (v1, v2) in
        v1.leading == v2.leading
        v1.bottom == v2.bottom
        v1.trailing == v2.trailing
        v1.top == v2.top
    }

    self.playerViewController.didMoveToParentViewController(self)
    self.playerViewController.addObserver(self,
      forKeyPath: KeyPath.ReadyForDisplay, options: nil, context: nil)
  }
}

override func observeValueForKeyPath(keyPath: String,
  ofObject object: AnyObject, change: [NSObject : AnyObject],
  context: UnsafeMutablePointer<Void>) {
    if keyPath == KeyPath.ReadyForDisplay {
      dispatch_async(dispatch_get_main_queue()) {
      self.finishConstructingInterface()
    }
  }
}

func finishConstructingInterface() {
  if playerViewController.readyForDisplay == false {
    return
  }
  playerViewController.removeObserver(self, forKeyPath: KeyPath.ReadyForDisplay)
  playerViewController.view.hidden = false
  playerViewController.view.userInteractionEnabled = true
}

这种效果很好,播放器按预期工作,但我遇到了一个奇怪的问题:它的默认界面不响应触摸。为了理解这个问题,我查看了 View 调试器,我发现顶部的 AVTouchIgnoringView 阻塞了界面:

AVTouchIgnoringView on the top

所以我的问题如下:AVTouchIgnoringView 是什么,为什么它会干扰视频播放器界面?以及如何摆脱它?也许有一些我只是没有看到的非常明显的原因?

感谢您的帮助!

最佳答案

好的,问题解决了,而且它与AVPlayerViewController完全无关。原因是我的播放器 View 容器是 UIImageView 的子类,overrides userInteractionEnabled 为否:

This property is inherited from the UIView parent class. This class changes the default value of this property to NO.

因此将此属性设置为 YES 可以解决此问题。不确定我是应该保留这个问题还是删除它,因为这会让读者有些困惑。

关于ios - 嵌入式 AVPlayerControllerView 添加 AVTouchIgnoringView 阻止默认播放器的界面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32226080/

相关文章:

ios - 将视频分成 30 秒的 block Ios swift 5

swift - 如何使用 AVPlayer 进行基本身份验证来播放服务器上的视频文件?

swift - AVPlayerViewController 框架上的 AVButton

ios - 在 mac 中启动 iOS 模拟器失败

swift - 从网址将视频加载到 View 时出现问题

ios - 将 Pods-acknowledgements.markdown 加载为 NSString

ios - 为什么 AVPlayer 边界时间观察器不起作用?

ios - 在 AVPlayerViewController 中点击播放按钮时执行操作

iphone - 从 HTML 字符串中读取并获取值

ios - 如何正确处理 CVPixelBuffer 中的像素?