objective c - Loop a segment of a video with AVPlayer -


i trying loop segment of video, given 2 frame markers (markin , markout). when option loop selected, player loop segment of video. have loop whole video set using apple's suggestion of sending avplayeritemdidplaytoendtimenotification once end reached.

what think clean way implement send notification when markout point reached, if looping activated move player markin point. there way create notification along of lines of playeritemdidreachmarkout?

i'm new notifications , avplayer, forgive me if i'm missing something.

what you're looking called boundary time observer. give avplayer list of cmtimes, , notify when player's currenttime approximately of times.

it works this:

//use unretained reference in block break retain cycle of player retaining block retaining 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, must use removetimeobserver: tell avplayer stop observation. give object got addboundarytimeobserver…:::.

notes/caveats/warnings

  • despite name, not have “boundary” times. can have 1 time, , when don't, avplayer makes no inferences whether of times start time, end time, midpoint, or whatever.
  • the same block may called multiple times same “boundary”. make sure handle appropriately (in case, had make sure not show same subtitle twice).
  • boundary time observers not called when seeking (again, times not interpreted “boundaries” in sense of start , end). if seek straight boundary time (more or less—see next point), should notified that, seeking point in between 2 boundaries, or among many boundaries, not cause observation.
  • i said approximately, , mean it. main case in i've seen avplayer notify multiple times when avplayer notifies little early, , notifies again @ (or @ least closer to) exact time. don't assume currenttime equal time supplied.

Comments