AVPlayer removes periodicTimeObserver

I find it hard to stop the AVPlayers time watcher.

I have an AVPlayer that works 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'm done with the player, I do this:

[player pause];
[player removeTimeObserver:self.timeObserver]
player = nil;

      

strangely when I put a breakpoint in the code and execute the code using XCode it works. I can see that the block stops printing "observer code"

But when I run the code normally without breakpoints, I see that the observer is still running at the same interval after calling [player removeTimeObserver].

Any ideas?

+3


source to share


1 answer


Glad to see weakSelf worked for you ...

In the above example, I don't see you assigning the result

  [player addPeriodicTimeObserverForInterval ...

      

to self.timeObserver it might be a typo, but it should be;



   self.timeObserver =  [player addPeriodicTimeObserverForInterval ...

      

if you intend to call

   [player removeTimeObserver:self.timeObserver]; 

      

you also missed the ";" higher.

+17


source







All Articles