CurrentPlaybackTime & NSTimer starts views during video and starts / restarts correctly when the time point is located using a scrubber

Fighting here ...

I try to display views in certain periods on the video timeline, being replaced when I start the next view. I was very successful - my problem is that this happens only linearly, and if the game head returns to a view that has already been started (i.e., something to this point), the timer continues, but the triggers no longer shoot.

Any help would be greatly appreciated! Here is the code ...

- (void)viewDidLoad { [super viewDidLoad]; keyframeTimes = [[NSMutableArray alloc] init]; shoutOutTexts = [[NSArray arrayWithObjects:@"This is a test\nLabel at 2 secs ", @"This is a test\nLabel at 325 secs", nil] retain]; shoutOutTimes = [[NSArray arrayWithObjects:[[NSNumber alloc] initWithInt: 2], [[NSNumber alloc] initWithInt: 325], nil] retain]; self.player = [[MPMoviePlayerController alloc] init]; self.player.contentURL = [self movieURL]; // END:viewDidLoad1 self.player.view.frame = self.viewForMovie.bounds; self.player.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; [self.viewForMovie addSubview:player.view]; [self.player play]; // START_HIGHLIGHT [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(timerAction:) userInfo:nil repeats:YES]; // END_HIGHLIGHT // START:viewDidLoad1 [self.view addSubview:self.myScrollView]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(movieDurationAvailable:) name:MPMovieDurationAvailableNotification object:nil]; } // END:viewDidLoad // END:viewDidLoad1 // START:movieURL -(NSURL *)movieURL { NSBundle *bundle = [NSBundle mainBundle]; NSString *moviePath = [bundle pathForResource:@"BigBuckBunny_640x360" ofType:@"m4v"]; if (moviePath) { return [NSURL fileURLWithPath:moviePath]; } else { return nil; } } // END:movieURL int position = 0; - (void)timerAction:(NSTimer*)theTimer { NSLog(@"hi"); int count = [shoutOutTimes count]; NSLog(@"count is at %d", count); if (position < count) { NSNumber *timeObj = [shoutOutTimes objectAtIndex:position]; int time = [timeObj intValue]; NSLog(@"time is at %d", time); if (self.player.currentPlaybackTime >= time) { CommentView *cview = [[CommentView alloc] initWithText:[shoutOutTexts objectAtIndex:position]]; [self.player.view addSubview:cview]; position++; [NSTimer scheduledTimerWithTimeInterval:4.0f target:self selector:@selector(removeView:) userInfo:cview repeats:NO]; } } } - (void)removeView:(NSTimer*)theTimer { UIView *view = [theTimer userInfo]; [view removeFromSuperview]; } 

Here the magazine calls Till ...

Here the magazines are ringing ...

 2011-04-23 11:53:44.370 MoviePlayer[17129:207] last check was at 7.76279 2011-04-23 11:53:44.371 MoviePlayer[17129:207] current playback time is 8.76292 2011-04-23 11:53:44.371 MoviePlayer[17129:207] shouting: This is a test Label at 2 secs 2011-04-23 11:53:45.368 MoviePlayer[17129:207] position is at 2 2011-04-23 11:53:45.369 MoviePlayer[17129:207] shout scheduled for 8 2011-04-23 11:53:45.370 MoviePlayer[17129:207] last check was at 8.76451 2011-04-23 11:53:45.371 MoviePlayer[17129:207] current playback time is 9.76299 
+4
source share
1 answer

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; } 
+2
source

All Articles