I am trying to loop a segment of video, given two frame markers (markIn and markOut). When the option to loop is selected, the player will loop this segment of the video. I currently have a loop for the whole video set up using Apple's suggestion of sending the AVPlayerItemDidPlayToEndTimeNotification once the end is reached.
What I think will be a clean way to implement this will be to send a notification when the markOut point is reached, if looping is activated it will move the player back to the markIn point. So is there are way to create a notification along of the lines of playerItemDidReachMarkOut?
I'm fairly new to notifications and AVPlayer, so forgive me if I'm missing something.
What you're looking for is called a boundary time observer. You give your AVPlayer a list of CMTime
s, and it will notify you when the player's currentTime
is approximately any of those times.
It works like this:
//Use an unretained reference in the block to break the retain cycle of the player retaining the block retaining the player retaining…
__unsafe_unretained AVPlayer *weakPlayer = _myPlayer;
_myObserver = [_myPlayer addBoundaryTimeObserverForTimes:@[ [NSValue valueWithCMTime:markOutTime] ]
queue:dispatch_get_main_queue()
usingBlock:^{
[weakPlayer seekToTime:markInTime
/*optional:
toleranceBefore:kCMTimeZero
toleranceAfter:kCMTimeZero
*/
];
}
];
Later, of course, you must use removeTimeObserver:
to tell the AVPlayer to stop this observation. You give it the object you got from addBoundaryTimeObserver…:::
.
currentTime
will be exactly equal to any time you supplied.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With