gpt4 book ai didi

ios - 删除 periodicTimeObserver 的 AVPlayer

转载 作者:可可西里 更新时间:2023-11-01 06:18:09 24 4
gpt4 key购买 nike

我很难停止 AVPlayers 时间观察器。

我有一个像这样运行的 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 单步执行代码时,它起作用了。我可以看到该 block 停止打印出“观察者代码”

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

有什么想法吗?

最佳答案

很高兴看到 weakSelf 为你工作......

在上面我没有看到你分配

的结果
  [player addPeriodicTimeObserverForInterval ...

对于 self.timeObserver 来说,这可能是一个错字,但它应该是;

   self.timeObserver =  [player addPeriodicTimeObserverForInterval ...

如果你打算打电话

   [player removeTimeObserver:self.timeObserver]; 

你也错过了“;”以上。

关于ios - 删除 periodicTimeObserver 的 AVPlayer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26068025/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com