Hello, I am currently trying to synchronize two or more AVPlayers, each with a different player item (camera angle), but I am unable to get all of the player to start playing at the same time. I am also working with live video, so I don't think I can use AVCompositions.
Current Procedure:
- Pause all players
- Seek all players to the desired time
- If all seeks completed then Preroll all players
- If all prerolls completed then tell all players to Play
Code:
- (void)syncAtTime:(CMTime)time { __block NSArray *players = self.players; const NSUInteger prerollsNeeded = players.count; __block NSUInteger prerollsTried = 0; __block NSUInteger prerollsCompleted = 0; void (^prerollHandler)(BOOL) = ^(BOOL complete) { prerollsTried++; if (complete) { prerollsCompleted++; } if (prerollsTried >= prerollsNeeded) { // all preroll completion handlers have ran // play all the players now that they SHOULD be ready if (prerollsCompleted >= prerollsNeeded) { for (AVPlayer *player in players) { [player play]; } } } }; const NSUInteger seeksNeeded = players.count; __block NSUInteger seeksTried = 0; __block NSUInteger seeksCompleted = 0; void (^seekHandler)(BOOL) = ^(BOOL complete) { seeksTried++; if (complete) { seeksCompleted++; } if (seeksTried >= seeksNeeded) { // all seek completion handlers have ran // if all the seeks completed preroll and play if (seeksComplete >= seeksNeeded) { for (AVPlayer *player in players) { [player prerollAtRate:1.0 completionHandler:prerollHandler]; } } } }; // pause seek all the players for (AVPlayer *player in players) { [player pause]; [player seekToTime:time toleranceBefore:kCMTimeZero toleranceAfter:kCMTimeZero completionHandler:seekHandler]; } }
If anyone has any tips or suggestions please let me know!
(Yes I know I should be using Swift) 🙂
Update:
So I ended up using one of my technical support credits on this question, but the results weren't very pleasing regarding HTTP Live Streaming (HLS).
"Our engineers have reviewed your request and have concluded that there is no supported way to achieve the desired functionality given the currently shipping system configurations" - Apple Developer Technical Support
I guess I'll start a new post under HTTP Live Streaming, since that is the only issue now.