AVPlayer,播放/暂停状态通知?

我正在寻找一种方式来获得AVPlayer开始播放的确切时间。 有“rate”属性,但是目前我正在用NSTimer定期检查它以获取更新。

我试过KVO,但显然不符合KVO标准。

我知道当玩家ENDED时有事件发生 。 但是我在这里说暂停。

我也是KVO订阅了AVPlayerItem's “状态”,但是当HTTP资源完成caching,没有播放/暂停时,它显示我。 我也开始收集播放/暂停的所有电话,之后请求即时UI更新,但是在AVPlayer真正开始播放之前,需要更多的循环播放。 我只是想立即更新我的button。

对于我OS 10以上您可以检查AVPlayer timeControlStatus的新属性。

 if(avplayerObject.timeControlStatus == AVPlayerTimeControlStatusPaused) { //Paused mode } else if(avplayerObject.AVPlayerTimeControlStatusPlaying) { //Play mode } 

你为什么说“率”不是KVO投诉? 这个对我有用。

这是我做的:

 - (void)viewDidLoad { ... [self.player addObserver:self forKeyPath:@"rate" options:0 context:nil]; } 

接着:

 - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { if ([keyPath isEqualToString:@"rate"]) { if ([self.player rate]) { [self changeToPause]; // This changes the button to Pause } else { [self changeToPlay]; // This changes the button to Play } } } 

AVPalyer作为默认观察者来跟踪video的当前持续时间,当您暂停或恢复video时,您可以通过使用一个全局variables获得暂停时间(在观察者内部更新该variables)

CMTime interval = CMTimeMake(1,1);

 //The capture of self here is coming in with your implicit property access of self.currentduration - you can't refer to self or properties on self from within a block that will be strongly retained by self. //You can get around this by creating a weak reference to self before accessing timerDisp inside your block __weak typeof(self) weakSelf = self; self.timeObserverToken = [_player addPeriodicTimeObserverForInterval:interval queue:NULL usingBlock: ^(CMTime time) { _currentDuration = (int)CMTimeGetSeconds (_player.currentTime); if(!_isPlaying) { _pausedDuration = _currentDuration; } }