简体   繁体   English

两个AVPlayer视频不同步(快速)

[英]Two AVPlayer videos out of sync (Swift)

I have two AVPlayer() items playing videos of the same duration (10 seconds). 我有两个AVPlayer()项播放相同持续时间(10秒)的视频。 The goal is to have them loop and stay in sync with one another. 目标是让它们循环并彼此保持同步。 I add them as sublayers of the same UIView and then call player.play() on each one of them. 我将它们添加为同一UIView子层,然后在它们中的每一个上调用player.play()

The problem though is that as code execution obviously has the slightest delay as one is called after the other one, the videos are out of sync (although only a few milliseconds, it is noticeable). 但是,问题在于,由于代码执行显然有最小的延迟,因为一个延迟在另一个延迟之后被调用,因此视频不同步(尽管只有几毫秒,但很明显)。

I do not have the option to create an AVMutableComposition as I have seen other posts suggest, so is there anyway to have two separate players truly stay in sync and play EXACTLY at the same time? 正如我看到的其他帖子所建议的那样,我没有选择创建AVMutableComposition的方法,因此,是否有两个真正独立的播放器真正保持同步并同时播放?

Thank you! 谢谢!

If you want to achieve the sync, you should load the videos separately with AVPlayer and observe the AVPlayerItemStatus property of each player. 如果要实现同步,则应分别使用AVPlayer加载视频并观察每个播放器的AVPlayerItemStatus属性。 Only when all of the players have the status .readyToPlay you can loop through the players and set the .rate property. 只有当所有的球员都有状态.readyToPlay可以遍历球员和设置.rate属性。

Edit: 编辑:

You can also synchronize them by using setRate(_:time:atHostTime:) . 您也可以使用setRate(_:time:atHostTime:)同步它们。 Don't forget begin loading media data using preroll(atRate:completionHandler:) before calling setRate . 不要忘记在调用setRate之前开始使用preroll(atRate:completionHandler:)加载媒体数据。 Basically: 基本上:

  • wait for readyToPlay 等待readyToPlay
  • preroll(atRate:completionHandler:) when all players are ready 当所有玩家都准备好时preroll(atRate:completionHandler:)
  • setRate(_:time:atHostTime:) when all players were prerolled setRate(_:time:atHostTime:)当所有玩家都已预滚动时

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

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