AVQueuePlayer的play/pause方法是异步调用的,因此更新UI的方法可能会在UI线程之前或之后调用。为了确保更新UI的方法在执行这些方法后立即调用,可以使用KVO观察AVQueuePlayer的rate属性,在值变化时手动更新UI。例如:
// 在初始化AVQueuePlayer时添加观察器
[self.player addObserver:self forKeyPath:@"rate" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
// 在KVO观察器中检查AVQueuePlayer的rate值并更新UI
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if ([keyPath isEqualToString:@"rate"]) {
float rate = [change[NSKeyValueChangeNewKey] floatValue];
if (rate == 0.0) {
// AVQueuePlayer暂停
// 更新UI
} else {
// AVQueuePlayer播放
// 更新UI
}
}
}