iOS AVPlayer:使用 addBoundaryTimeObserver() 在时间戳处暂停()视频仅在 99% 的时间内准确

问题描述 投票:0回答:1

99% 的情况下这都很有效。但对于 1% 的失败,视频将在一个不可接受的遥远时间戳处暂停。

self.player.addBoundaryTimeObserver(forTimes: [NSValue(time: CMTime(seconds: timeStamp, preferredTimescale: (player.currentItem?.asset.duration.timescale)!))], queue: DispatchQueue.main, using: { [unowned self] in

            self.player.pause()

})

失败中没有明显的模式。

我有一个 hacky 解决方案,涉及将视频分成两个单独的视频,然后当第一个视频结束时会发生暂停......但我真的只是希望这个简单的代码能够 100% 地工作。

也许苹果未来会完善这些功能?我过去见过 AVPlayer 未经宣布的改进。

以下是 Apple 的函数文档: https://developer.apple.com/documentation/avfoundation/avplayer/1388027-addboundarytimeobserver https://developer.apple.com/documentation/avfoundation/avplayer/1387895-pause

我绝对不想在这里宣传我的应用程序,但您可以在 iOS 应用程序商店的我的应用程序中看到此错误。该应用程序名为“Voomoo”。

ios swift uikit avplayer
1个回答
0
投票

如果您在这里阅读 Apple 文档:

https://developer.apple.com/documentation/avfoundation/avplayer/1388027-addboundarytimeobserver

这里已经明确提到了:

“边界时间是您在媒体时间轴内定义的任意兴趣点。由于在正常播放期间会遍历这些时间,因此将调用您提供给此方法的块。您必须维护对返回值的强引用,只要您希望玩家调用时间观察器。此方法的每次调用都应与对removeTimeObserver(_:) 的相应调用配对。”

尝试更新您的代码,例如:

self.timelineObserver = self.player.addBoundaryTimeObserver(forTimes: [NSValue(time: CMTime(seconds: timeStamp, preferredTimescale: (player.currentItem?.asset.duration.timescale)!))], queue: DispatchQueue.main, using: { [unowned self] in

            self.player.pause()

})

这样“timelineObserver”就会被保留,你的观察者肯定会被调用。您也可以稍后在

removeTimeObserver(_:)
方法中使用“timelineObserver”来保持添加和删除观察者之间的平衡。

© www.soinside.com 2019 - 2024. All rights reserved.