2011-09-27 12 views
23

Estoy buscando una manera de ser notificado el momento exacto cuando AVPlayer comienza a jugar. Está la propiedad de "tasa", pero actualmente la estoy verificando periódicamente con un NSTimer para obtener actualizaciones.AVPlayer, notificación de estado de reproducción/pausa?

Intenté KVO, pero aparentemente no es compatible con KVO.

Sé que hay events cuando el jugador ENDED. Pero estoy hablando de pausa aquí.

También me he suscrito a AVPlayerItem's "estado", pero me está mostrando cuando el activo HTTP ha terminado de almacenar en caché, no reproducir/pausar. También comencé a recopilar todas las llamadas de play/pausa, solicitando una actualización de UI instantánea más tarde, pero se necesitan más runloops antes de que realmente empiece a reproducirse el AVPlayer. Me encantaría actualizar mi botón al instante.

+0

Hay una manera de controlar si el AVPlayer está jugando figuran en esta lista: http://stackoverflow.com/a/9288642/2383604 –

Respuesta

5

Para i OS 10 en adelante Puede comprobar nueva propiedad de AVPlayer timeControlStatus.

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

¿Por qué dice que "tasa" no es una queja de KVO? Funciona para mí.

Esto es lo que hice:

- (void)viewDidLoad 
{ 
    ... 

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

Y luego:

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

realmente? ¿Intentó eso en iOS4 o iOS5? Volveré a hacer mis pruebas; tal vez fue solo mi error después de todo. – steipete

+0

Lo probé en iOS4. No has probado iOS5. – raixer

+3

Lo probé en iOS5. Para llamar a mi observador, tuve que agregar al observador con opciones: 'NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew'. –

6

AVPalyer como observador por defecto para realizar un seguimiento de la duración actual del vídeo, cuando hace una pausa o reanudar el video se puede obtener Tiempo de pausa mediante el uso de una variable global (dentro de la actualización del observador esa variable)

CMTiempo 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; 
    } 
} 
Cuestiones relacionadas