2011-09-27 9 views
23

Sto cercando un modo per ricevere la notifica del momento esatto in cui inizia il gioco AVPlayer. C'è la proprietà "rate", ma attualmente la sto controllando periodicamente con un NSTimer per ottenere gli aggiornamenti.AVPlayer, notifica per stato di riproduzione/pausa?

Ho provato KVO, ma a quanto pare non è conforme a KVO.

So che ci sono events quando il lettore ENDED. Ma sto parlando di pausa qui.

Inoltre, KVO ha sottoscritto lo AVPlayerItem's "stato", ma mi mostra quando l'asset HTTP ha terminato la memorizzazione nella cache, nessuna riproduzione/pausa. Ho anche iniziato a raccogliere tutte le chiamate di riproduzione/pausa, richiedendo un aggiornamento dell'interfaccia utente istantanea in seguito, ma ci vogliono alcuni più runloop prima che lo AVPlayer inizi realmente a giocare. Mi piacerebbe semplicemente aggiornare il mio pulsante all'istante.

+0

C'è un modo per controllare se l'AVPlayer sta giocando elencati qui: http://stackoverflow.com/a/9288642/2383604 –

risposta

5

Per i OS 10 in poi È possibile controllare nuova proprietà di AVPlayer timeControlStatus.

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

Perché dici che "tasso" non è un reclamo KVO? Funziona per me.

Ecco quello che ho fatto:

- (void)viewDidLoad 
{ 
    ... 

    [self.player addObserver:self forKeyPath:@"rate" options:0 context:nil]; 
} 

E poi:

- (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

davvero? l'hai provato su iOS4 o iOS5? Rifonderà i miei test; forse era solo un mio errore, dopotutto. – steipete

+0

L'ho provato su iOS4. Non ho provato iOS5. – raixer

+3

L'ho provato su iOS5. Per chiamare il mio osservatore, ho dovuto aggiungere l'osservatore con le opzioni: 'NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew'. –

6

AVPalyer in qualità di osservatore di default per monitorare la durata corrente del video, quando si mette in pausa o riprendere il video si può ottenere tempo di pausa utilizzando una variabile globale (all'interno dell'osservatore aggiornare tale variabile)

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