Two AVPlayer videos out of sync (Swift)

旧巷老猫 提交于 2019-12-22 01:39:19

问题


I have two AVPlayer() items playing videos of the same duration (10 seconds). 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.

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?

Thank you!


回答1:


If you want to achieve the sync, you should load the videos separately with AVPlayer and observe the AVPlayerItemStatus property of each player. Only when all of the players have the status .readyToPlay you can loop through the players and set the .rate property.

Edit:

You can also synchronize them by using setRate(_:time:atHostTime:). Don't forget begin loading media data using preroll(atRate:completionHandler:) before calling setRate. Basically:

  • wait for readyToPlay
  • preroll(atRate:completionHandler:) when all players are ready
  • setRate(_:time:atHostTime:) when all players were prerolled


来源:https://stackoverflow.com/questions/45550032/two-avplayer-videos-out-of-sync-swift

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!