ios - 如何使用 AVPlayerViewController 检测播放控件显示的切换?

标签 ios swift avplayer avplayerviewcontroller

我想知道是否可以检测播放控件何时从 AVPlayerViewController View 中出现或消失。
我正在尝试在我的播放器上添加一个必须遵循播放控件显示的 UI 元素。仅在显示控件时出现,否则消失

我似乎没有找到任何可以在 AVPlayerViewController 上观察到的值来实现这一点,也没有找到任何回调或委托(delegate)方法。

我的项目在 Swift 中。

最佳答案

观察和响应播放变化的一种简单方法是使用 键值观察 (KVO) .在你的情况下,观察 AVPlayer 的 timeControlStatusrate属性(property)。

例如。:

{
  // 1. Setup AVPlayerViewController instance (playerViewController)

  // 2. Setup AVPlayer instance & assign it to playerViewController

  // 3. Register self as an observer of the player's `timeControlStatus` property

  // 3.1. Objectice-C
  [player addObserver:self
           forKeyPath:@"timeControlStatus"
              options:NSKeyValueObservingOptionOld|NSKeyValueObservingOptionNew // NSKeyValueObservingOptionOld is optional here
              context:NULL];

  // 3.2. Swift
  player.addObserver(self,
                     forKeyPath: #keyPath(AVPlayer.timeControlStatus),
                     options: [.old, .new], // .old is optional here
                     context: NULL)
}

要收到状态更改的通知,请实现 -observeValueForKeyPath:ofObject:change:context:方法。每当 timeControlStatus 时调用此方法值变化。
// Objective-C
- (void)observeValueForKeyPath:(NSString *)keyPath
                      ofObject:(id)object
                        change:(NSDictionary <NSKeyValueChangeKey, id> *)change
                       context:(void *)context
{
  if ([keyPath isEqualToString:@"timeControlStatus"]) {
    // Update your custom UI here depend on the value of `change[NSKeyValueChangeNewKey]`:
    // - AVPlayerTimeControlStatusPaused
    // - AVPlayerTimeControlStatusWaitingToPlayAtSpecifiedRate
    // - AVPlayerTimeControlStatusPlaying
    AVPlayerTimeControlStatus timeControlStatus = (AVPlayerTimeControlStatus)[change[NSKeyValueChangeNewKey] integerValue];
    // ...

  } else {
    [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
  }
}

// Swift
override func observeValue(forKeyPath keyPath: String?,
                           of object: Any?,
                           change: [NSKeyValueChangeKey : Any]?,
                           context: UnsafeMutableRawPointer?)
{
  if keyPath == #keyPath(AVPlayer.timeControlStatus) {
    // Deal w/ `change?[.newKey]`
  } else {
    super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
  }
}

最后最重要的一步 ,当你不再需要观察者时,记得移除它,一般在-dealloc :
[playerViewController.player removeObserver:self forKeyPath:@"timeControlStatus"];

顺便说一句,您还可以观察 AVPlayer 的 rate属性(property),原因 -play相当于将 rate 的值设置为 1.0 和 -pause相当于将 rate 的值设置为 0.0。

但在你的情况下,我认为 timeControlStatus更有意义。

有一个官方文档供进一步阅读(但只是“准备播放”、“失败”和“未知”状态,在这里没用):"Responding to Playback State Changes" .

希望能帮助到你。

关于ios - 如何使用 AVPlayerViewController 检测播放控件显示的切换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55125344/

相关文章:

arrays - 使用sqlite数据库将字符串转换为json数组

ios - 如何以编程方式获取 iOS 上粘贴板内容的来源

iphone - iOS后台音频链接器错误

ios - 如何多次使用audioPlayerDidFinishPlaying

ios - TableView中, "cell.selected = true "这句话后整个表格都不能再点击了

ios - 在 UICollectionView 中滚动和选择时出现奇怪的行为

ios - Swift 4 覆盖 func viewDidAppear(_ 动画 : Bool) not triggering when i dissmiss viewcontroller

ios - 创建检查将产生错误的按钮(swift3)

ios - 为什么 UITextInputMode.activeInputModes() 在 Swift 上崩溃?

objective-c - AVFoundation,如何显示字幕?