2011-09-27 83 views
23

我正在寻找一种方法来获得AVPlayer开始播放的确切时间。有“费率”属性,但目前我正在使用NSTimer定期检查以获取更新。AVPlayer,播放/暂停状态通知?

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

我知道有events的时候玩家ENDED。但我在这里谈论暂停。

我也KVO订阅AVPlayerItem's“状态”,但它显示HTTP资产完成缓存,无播放/暂停时。我也开始收集播放/暂停的所有电话,之后请求即时UI更新,但在AVPlayer真的开始播放之前,需要更多的循环播放。我只是喜欢立即更新我的按钮

+0

有一种方法来监视是否AVPlayer播放这里列出:http://stackoverflow.com/a/9288642/2383604 –

回答

5

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

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

为什么你说“费率”不是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 
    } 
} 
} 
+0

真的吗?你在iOS4或iOS5上尝试过吗?将重新做我的测试;也许这毕竟只是我的错误。 – steipete

+0

我在iOS4上试过了。还没有试过iOS5。 – raixer

+3

我在iOS5中试过。为了让我的观察者被调用,我不得不添加观察者的选项:'NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew'。 –

6

AVPalyer默认观察员跟踪视频的当前时间,当您暂停或恢复播放视频,你可以得到通过使用一个全局变量来暂停时间(在观察者更新该变量的情况下)

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; 
    } 
}