Keeping two AVPlayers in sync
Asked Answered
C

1

7

I have a client who has a very specific request for the app that requires two AVPlayers to be in sync. One video is for some content and the other one is for a presenter speaking about the content. Using a AVMutableComposition to combine them into one video is not an option because the presenter video has to be able to respond to user generated events (e.g. they want to have a feature to show/hide the presenter) and I don't believe there is a way to have that kind of control over a specific AVMutableCompositionTrack.

So, I'm left with figuring out how to ensure that two AVPlayers stay in sync and I was wondering if anyone has had experience with this or suggestions for other tools to accomplish this.

Thanks

Crass answered 7/4, 2012 at 19:23 Comment(3)
Have you tried putting the players' AVPlayerLayers into the same AVSynchronizedLayer? (note: I haven’t tried this yet)Siana
I believe I tried that. I think the AVSynchronizedLayer was meant for keeping animations in sync with one player. I ended up manually starting/stopping both players. Then the client changed the spec which made it all moot.Crass
D’oh! I hate it when that happens! Anyway, that's my current approach as well. It's a pain because I seek around in one of the players a lot. Would love to have a simpler solution.Siana
F
1

The following methods are the ones to use

- (void)setRate:(float)rate 
           time:(CMTime)itemTime 
     atHostTime:(CMTime)hostClockTime;

- (void)prerollAtRate:(float)rate 
    completionHandler:(void (^)(BOOL finished))completionHandler;

Caveats

Important This method is not currently supported for HTTP Live Streaming or when automaticallyWaitsToMinimizeStalling is YES. For clients linked against iOS 10.0 and later or macOS 10.12 and later, invoking this method when automaticallyWaitsToMinimizeStalling is YES will raise an NSInvalidArgument exception.

This is an expected behavior since "live" is "present" and cannot be seek forward and setting the rate to less than 1.0 it will cause to extra buffering the stream (second point is a guess).

Documentation

https://developer.apple.com/documentation/avfoundation/avplayer/1386591-setrate?language=objc

https://developer.apple.com/documentation/avfoundation/avplayer/1389712-prerollatrate?language=objc

As a side note consider that HLS streams are not truly live streams, the "present moment" could vary several seconds among clientes consuming the stream, the opposite of WebRTC for example where the delay between publishers and consumers is kinda of warranted for 1 second max.

Filamentary answered 22/9, 2017 at 18:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.