简体   繁体   中英

Two AVPlayer videos out of sync (Swift)

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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