2011-09-27 81 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; 
    } 
}