views:

53

answers:

3

I want to use the property currentPlaybackTime, which returns a double value to trigger a series of if statements at certain points throughout the movie's playback to pause it. Because of the approximate behavior of doubles, it never syncs up with the value in my if statement, and the movie doesn't stop. I can't use a range though because when I resume playing, it might still be within the range an immediately pause again.

SO I think what I need to do is round this double value to 2 decimal places.

But how do I observe each change to the currentPlaybackTime property? Right now I have a timer set to call the method checking my if statements against the playback time every .0333 seconds(approx 30 times a second(my movies framerate)). However this is inexact and I feel almost certainly wrong.

+2  A: 

Perfect time to check out Key-Value Observing.

Jacob Relkin
A: 

Cool, So I register the observer on viewDidLoad

[player addObserver:self forKeyPath:@"currentPlaybackTime" options:(NSKeyValueObservingOptionNew) context:NULL];

Then add

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
    //[self printTimecode];
    if (player.currentPlaybackTime == 4) {
        bookmark=1;
        [self pauseVideo];
    }    

This however is still not working. What have I done wrong?

Hippocrates
A: 

I'm not sure to what the property you're attempting to observe belongs, but it must be KVO compliant for this approach to work. That isn't a given for every property.

Internally, the code providing you currentPlaybackTime must call willChangeValueForKey: and didChangeValueForKey: (if it uses KVC to change the property, it gets these calls for free). I'd check the documentation (or the code if it's yours) to ensure this is observable.

paulbailey