This is because position is a static variable, and you always raise it, but don't lower it.
You will need to slightly change the logic in the timer function - perhaps like this:
Remove the static variable position from your source ( int position = 0; )
- (NSInteger)positionFromPlaybackTime:(NSTimeInterval)playbackTime { NSInteger position = 0; for (NSNumber *startsAt in shoutOutTimes) { if (playbackTime > [startsAt floatValue]) { ++position; } } return position; }
Add a static variable (actually the instance variable will be cleaner, but hey, we're just trying to get it working at the moment):
NSTimeInterval lastCheckAt = -1.0;
Then change your timer method to this:
- (void)timerAction:(NSTimer*)theTimer { int count = [shoutOutTimes count]; NSInteger position = [self positionFromPlaybackTime:self.player.currentPlaybackTime]; NSLog(@"position is at %d", position); if (position > 0) { --position; } if (position < count) { NSNumber *timeObj = [shoutOutTimes objectAtIndex:position]; int time = [timeObj intValue]; NSLog(@"shout scheduled for %d", time); NSLog(@"last check was at %g", lastCheckAt); NSLog(@"current playback time is %g", self.player.currentPlaybackTime); if (lastCheckAt < time && self.player.currentPlaybackTime >= time) { NSString *shoutString = [shoutOutTexts objectAtIndex:position]; NSLog(@"shouting: %@", shoutString); CommentView *cview = [[CommentView alloc] initWithText:shoutString]; [self.player.view addSubview:cview]; [NSTimer scheduledTimerWithTimeInterval:4.0f target:self selector:@selector(removeView:) userInfo:cview repeats:NO]; } } lastCheckAt = self.player.currentPlaybackTime; }
source share