gpt4 book ai didi

ios - 在 iOS AVPlayer 中,addPeriodicTimeObserverForInterval 似乎丢失了

转载 作者:搜寻专家 更新时间:2023-10-31 21:59:29 25 4
gpt4 key购买 nike

我正在尝试设置 AVPlayer.addPeriodicTimeObserverForInterval()。有人用过这个成功吗?

我正在使用 Xcode 8.1、Swift 3

最佳答案

接受的答案让人觉得您可以将返回值分配给局部变量并忽略它。但是根据doc ,实际上重要的是保持对返回值和 removeTimeObserver(_ :) 的强引用。

You must maintain a strong reference the returned value as long as you want the time observer to be invoked by the player. Each invocation of this method should be paired with a corresponding call to removeTimeObserver(:) . Releasing the observer object without invoking removeTimeObserver(:) will result in undefined behaviour.

所以我会这样做:

if let ob = self.observer {
player.removeTimeObserver(ob)
}

let interval = CMTimeMake(1, 4) // 0.25 (1/4) seconds
self.observer = player.addPeriodicTimeObserver(forInterval: interval, queue: DispatchQueue.main) { [weak self] time in
...
}

关于ios - 在 iOS AVPlayer 中,addPeriodicTimeObserverForInterval 似乎丢失了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40472326/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com