2014-09-26 51 views
3

我很难停止AVPlayers时间观察者。AVPlayer删除periodicTimeObserver

我有一个AVPlayer播放器中运行是这样的:

player = [[AVPlayer alloc] initWithURL:[NSURL fileURLWithPath:path]]; 

然后我添加一个观察者

[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]; 

一次,我与球员完成了我这样做:

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

奇怪的是,当我在代码中放置一个断点并使用XCode执行代码时,它会起作用。我可以看到块停止打印“观察员代码”

但是,当我正常运行代码而没有断点时,我可以看到观察者仍在以相同的间隔运行[player removeTimeObserver]后被调用。

任何想法?

回答

16

很高兴看到weakSelf为你工作?

在上面我没有看到你的分配

[player addPeriodicTimeObserverForInterval ... 

的结果self.timeObserver它可能是一个错字,但它应该是的

self.timeObserver = [player addPeriodicTimeObserverForInterval ... 

,如果你打算叫

[player removeTimeObserver:self.timeObserver]; 

你也错过了 “;”以上。

+0

嗨是两个计数感谢。这不是一个错字,但已经发现我的错误。很高兴有记录在这里,但。再次感谢。 – 2014-09-29 13:38:47