Reputation: 1557
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!
Upvotes: 0
Views: 2288
Reputation: 916
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:
readyToPlay
preroll(atRate:completionHandler:)
when all players are readysetRate(_:time:atHostTime:)
when all players were prerolledUpvotes: 5