我有-updateUI方法的ViewController。 AVPlayer播放时需要定期调用。
所以这是在-viewDidLoad中(注意,self.track不是AVPlayer实例,它是类Track的实例,其方法与AVPlayer几乎相同):
__weak ViewController * selfWeak = self;
[self.track addPeriodicTimeObserverForInterval:CMTimeMake(1, 20)
queue:dispatch_get_main_queue()
usingBlock:^{
[selfWeak updateUI];
NSLog(@"THIS BLOCK DOESN'T EXECUTE");
}];
这是在Track Class中实现此方法:
- (void) addPeriodicTimeObserverForInterval:(CMTime)interval
queue:(dispatch_queue_t)queue
usingBlock:(void (^)(void))block
{
self.periodicObserverBlock = block;
self.observeQueue = queue;
self.observeInterval = interval;
self.waitingToObserve = YES;
}
财产声明:
typedef void(^observeHandler)();
@property (nonatomic, strong) observeHandler periodicObserverBlock;
@property (nonatomic) CMTime observeInterval;
@property (nonatomic) dispatch_queue_t observeQueue;
当AVPlayer准备就绪时,我正在保存它。当它准备好了,我这样做:
- (void) afterLoadingMethod
{
__weak Track *selfWeak = self;
[self.player addPeriodicTimeObserverForInterval:self.observeInterval
queue:self.observeQueue
usingBlock:^(CMTime t){
(void) selfWeak.periodicObserverBlock;
NSLog(@“THIS BLOCK EXECUTES”);
}];
}
问题是 - -updateUI的块不会执行。 (它标有NSLog(@“此块不执行))。阻止在Track类中执行调用。所以我认为问题存在于存储属性的某个地方
self.periodicObserverBlock = block;
或者在这里:
@property (nonatomic, strong) observeHandler periodicObserverBlock;
但我没想出来。 提前谢谢。
答案 0 :(得分:1)
您只是引用该块:
(void) selfWeak.periodicObserverBlock;
要调用块,请使用括号:
selfWeak.periodicObserverBlock();