-1

I have a timer that counts the time of my audio. I would put a label text on the screen when you get a time:

- (void)updateTime:(NSTimer *)timer
{

    currentLocation = audio.currentTime;
    [lblCurrentLocation setText:[NSString stringWithFormat:@"%.4f Sec",currentLocation]];

    if (currentLocation == 4.7404)
    {
        [lblMsg setText:@"Sound p 1"];
    }

}

This IF is to compare CurrentLocation with 4.7404 is not working. Does anyone have any idea?

Thank you

mskfisher
  • 3,291
  • 4
  • 35
  • 48
user722056
  • 101
  • 1
  • 5
  • Maybe currentTime is 4.7404000000000001 instead of 4.7404... – Macmade Nov 28 '11 at 23:49
  • 2
    Seriously, you've got a timer, so it's set to refresh at a (not accurate) time interval... How can you expect catching an exact moment as 4.7404?!? – Macmade Nov 28 '11 at 23:51
  • I need to get exact 4.7404 sec moment in my sound and show message in label. How can I do that? – user722056 Nov 29 '11 at 00:10

2 Answers2

1

You cannot reliably get the timer to call your updateTime: method after exactly 4.7404 seconds. The iPhone does not guarantee any particular level of accuracy for timers.

Also, it takes time for iOS to turn your "Sound p 1" string into a bitmap and put it in the framebuffer. So even if you got called at exactly 4.7404 seconds, it would be later before the frame buffer was updated.

Also, the iPhone's LCD display has a non-zero response time. So after iOS updates the framebuffer, it will take time for the pixels on the screen to change.

Also, humans cannot detect changes at a .0001 second level of accuracy. So even if the pixels changed completely at exactly 4.7404 seconds, the user couldn't tell that it didn't happen at 4.739 seconds or at 4.741 seconds.

So what you are demanding is silly.

You should just display your message as soon as possible after at least 4.7404 seconds have elapsed:

- (void)updateTime:(NSTimer *)timer
{
    // Give your object a BOOL didSeeImportantTime property.
    if (!self.didSeeImportantTime) {
        currentLocation = audio.currentTime;
        if (currentLocation < 4.7404) {
            [lblCurrentLocation setText:[NSString stringWithFormat:@"%.4f Sec",currentLocation]];
        } else {
            self.didSeeImportantTime = YES;
            [lblMsg setText:@"Sound p 1"];
        }
    }
}
rob mayoff
  • 375,296
  • 67
  • 796
  • 848
0

You should compare floating point numbers differently. Here is a post on how to do it in C++. You can use it in Objective C.

Also, using a timer to try to get such an exact spot from the audio file is not very accurate.

Community
  • 1
  • 1
David V
  • 11,531
  • 5
  • 42
  • 66