I'm having difficulty stopping an AVPlayers time observer.
I have an AVPlayer player running like this:
player = [[AVPlayer alloc] initWithURL:[NSURL fileURLWithPath:path]];
then I add an observer
[player addPeriodicTimeObserverForInterval:CMTimeMake(3, 10) queue:NULL usingBlock:^(CMTime time){
NSTimeInterval seconds = CMTimeGetSeconds(time);
NSLog(@"observer called");
for (NSDictionary *item in robotR33) {
NSNumber *time = item[@"time"];
if ( seconds > [time doubleValue] && [time doubleValue] >= [lastTime doubleValue] ) {
// NSLog(@"LastTime: %qi", [lastTime longLongValue]);
lastTime = @(seconds);
NSString *str = item[@"line"];
[weakSelf nextLine:str];
// NSLog(@"item: %qi", [time longLongValue]);
// NSLog(@"Seconds: %f", seconds)
};
}
}];
[player play];
once I am finished with the player I do this:
[player pause];
[player removeTimeObserver:self.timeObserver]
player = nil;
the weird thing is when I put a breakpoint in the code and step through the code using XCode it works. I can see the block stops printing out "observer code"
But when I run the code normally with no breakpoints, I can see that the observer is still running at the same interval after the [player removeTimeObserver] has been called.
Any ideas?
Glad to see weakSelf worked for you ...
In the above I don't see you assigning the result of
[player addPeriodicTimeObserverForInterval ...
to self.timeObserver it may be a typo but it should be ;
self.timeObserver = [player addPeriodicTimeObserverForInterval ...
if you intend to call
[player removeTimeObserver:self.timeObserver];
you also missed the ";" above.