AVPlayer,播放/暂停状态通知?
我正在寻找一种方法来在 AVPlayer
开始播放的确切时刻收到通知。有“rate”属性,但目前我正在使用 NSTimer 定期检查它以获取更新。
我尝试了 KVO,但显然它不符合 KVO 标准。
我还通过 KVO 订阅了 AVPlayerItem 的“状态”,但它向我显示 HTTP 资源何时完成缓存,没有播放/暂停。我还开始收集所有播放/暂停的调用,然后请求即时 UI 更新,但在 AVPlayer
真正开始播放之前需要更多的运行循环。我只想立即更新我的按钮。
I'm searching for a way to get notified the exact moment when AVPlayer
starts playing. There's the "rate" property, but currently I am checking it periodically with an NSTimer
to get updates.
I tried KVO, but apparently it's not KVO compliant.
I know that there are events when the player ENDED. But i'm talking about pause here.
I also KVO subscribed to AVPlayerItem's
"status", but it's showing me when the HTTP asset has finished caching, no play/pause. I also started collecting all calls of play/pause, requesting an instant UI update afterwards, but it takes some more runloops before AVPlayer
really starts playing. I'd just love to update my button instantly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
迅速
in swift
将观察者添加到
AVPlayer
对象的rate
值:并覆盖
rate
更改时调用的方法:Add an observer to your
AVPlayer
object'srate
value:And override the method that will be called when the
rate
changes:需要向
AVPlayer
对象的rate
值添加观察者:重写以下方法以观察
rate
属性的变化Need to add an observer to
AVPlayer
object'srate
value:Override below method to observe changes in
rate
property为什么说“率”不是KVO的抱怨呢?
这对我有用。
这就是我所做的:
然后:
Why do you say that "rate" is not KVO complaint?
It works for me.
Here is what I did:
And then:
对于 iOS 10 及以上版本,您可以检查 AVPlayer timeControlStatus 的新属性。
For iOS 10 onwards You can check new property of AVPlayer timeControlStatus.
AVPalyer 作为默认观察者来跟踪视频的当前持续时间,当您暂停或恢复视频时,您可以通过使用一个全局变量来获取暂停时间(观察者内部更新该变量)
AVPalyer as default observer to track the current duration of the video ,when you pause or resume the video you can get paused time by using one global variable (inside observer update that variable)
如果您的目标是 iOS 13 及更高版本,则可以使用
Combine
优雅地实现此目的:其中
status
是AVPlayer 的任何
case
.TimeControlStatusIf you're targeting iOS 13 and up, you can pull this off elegantly using
Combine
:where
status
is anycase
ofAVPlayer.TimeControlStatus