简体   繁体   English

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

[英]How to detect the toggle on the display of playback controls with AVPlayerViewController?

I'd like to know if it is possible to detect when the playback controls appear or disappear from the AVPlayerViewController view.我想知道是否可以检测播放控件何时从 AVPlayerViewController 视图中出现或消失。 I'm trying to add a UI element on my player that must follow the playback controls display.我正在尝试在我的播放器上添加一个 UI 元素,该元素必须遵循播放控件显示。 Appearing only when the controls are displayed, disappearing otherwise仅在显示控件时出现,否则消失

I don't seem to find any value that I can observe on AVPlayerViewController to achieve this nor any callbacks or delegate methods.我似乎没有找到任何可以在 AVPlayerViewController 上观察到的值来实现这一点,也没有任何回调或委托方法。

My project is in Swift.我的项目在 Swift 中。

A simple way to observe and respond to the playback changes is to use key-value observing (KVO) .观察和响应播放变化的一种简单方法是使用键值观察(KVO) In your case, observe AVPlayer's timeControlStatus or rate property.在您的情况下,请观察 AVPlayer 的timeControlStatusrate属性。

eg:例如:

{
  // 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)
}

To be notified of state changes, you implement the -observeValueForKeyPath:ofObject:change:context: method.要获得状态更改的通知,请实现-observeValueForKeyPath:ofObject:change:context:方法。 This method is invoked whenever the timeControlStatus value changes.每当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)
  }
}

And last most important step , remember to remove the observer when you don't need it anymore, generally at -dealloc :最后最重要的一步,记住当你不再需要它时删除观察者,通常在-dealloc

[playerViewController.player removeObserver:self forKeyPath:@"timeControlStatus"];

Btw, you can also observe AVPlayer's rate property, cause -play equivalents to setting the value of rate to 1.0, and -pause equivalent to setting the value of rate to 0.0.顺便说一句,您还可以观察AVPlayer 的rate属性,导致-play等效于将rate 的值设置为1.0,而-pause等效于将rate 的值设置为0.0。

But in your case, I think timeControlStatus makes more sense.但在你的情况下,我认为timeControlStatus更有意义。


There's an official DOC for further reading (but just "Ready to Play", "Failed" & "Unknown" states, useless here): "Responding to Playback State Changes" .有一个官方文档供进一步阅读(但只是“准备播放”、“失败”和“未知”状态,在这里没用): “响应播放状态更改”

Hope it helps.希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM