Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4546984
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T16:01:54+00:00 2026-05-21T16:01:54+00:00

Struggling here… I am trying to have views displayed during specific periods in a

  • 0

Struggling here…

I am trying to have views displayed during specific periods in a videos timeline, being replaced when the next view is triggered. This much I have managed to do – my problem is that this only happens in a linear fashion and if the playhead is moved back to a view that has already been triggered (ie anything prior to that point) the timer continues on, but the triggers are no longer firing.

Any help will be greatly appreciated! Here’s 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’s the log calls Till…

Here’s the log calls…

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T16:01:55+00:00Added an answer on May 21, 2026 at 4:01 pm

    That happens because position is a static variable and you are always raising it but never lowering it.

    You will need to change the logic within the timer function a little – maybe 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 an instance variable would be cleaner but hey, we are just trying to get it to work at this 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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im struggling here for a lot, Im trying to upload an image from iphone
here's an easy one (that I'm struggling with)! I have a textarea, a button,
I have been struggling with this for months in my project. Here's the deal:
I'm really struggling with something here. I have a class module, let's call it
I'm struggling for a pattern here. I have quite a few tasks that need
after a couple of hours of struggling here I am. I have the following
Struggling with a SQL query of mine here. I have a table: APPS(id, game)
Here is the situation I am struggling with. I have an Object Model: public
I'm really struggling with basic Javascript here... basically, I'm trying to make a function
I'm struggling to find the right terminology here, but if you have jQuery object...

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.