Me gustaría saber si es posible detectar cuándo aparecen o desaparecen los controles de reproducción de la vista AVPlayerViewController. Estoy tratando de agregar un elemento de interfaz de usuario en mi reproductor que debe seguir la pantalla de controles de reproducción. Apareciendo solo cuando se muestran los controles, desapareciendo de lo contrario
Parece que no encuentro ningún valor que pueda observar en AVPlayerViewController para lograr esto ni devoluciones de llamada o métodos delegados.
Mi proyecto está en Swift.
Una forma sencilla de observar y responder a los cambios de reproducción es utilizar la observación de valores clave (KVO) . En su caso, observe la propiedad timeControlStatus o rate de AVPlayer.
p.ej:
{ // 1. Setup AVPlayerViewController instance (playerViewController) // 2. Setup AVPlayer instance & assign it to playerViewController // 3. Register self as an observer of the player's `timeControlStatus` property // 3.1. Objectice-C [player addObserver:self forKeyPath:@"timeControlStatus" options:NSKeyValueObservingOptionOld|NSKeyValueObservingOptionNew // NSKeyValueObservingOptionOld is optional here context:NULL]; // 3.2. Swift player.addObserver(self, forKeyPath: #keyPath(AVPlayer.timeControlStatus), options: [.old, .new], // .old is optional here context: NULL) } Para recibir notificaciones de cambios de estado, implemente el -observeValueForKeyPath:ofObject:change:context: Este método se invoca cada vez que cambia el valor de timeControlStatus .
// Objective-C - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary <NSKeyValueChangeKey, id> *)change context:(void *)context { if ([keyPath isEqualToString:@"timeControlStatus"]) { // Update your custom UI here depend on the value of `change[NSKeyValueChangeNewKey]`: // - AVPlayerTimeControlStatusPaused // - AVPlayerTimeControlStatusWaitingToPlayAtSpecifiedRate // - AVPlayerTimeControlStatusPlaying AVPlayerTimeControlStatus timeControlStatus = (AVPlayerTimeControlStatus)[change[NSKeyValueChangeNewKey] integerValue]; // ... } else { [super observeValueForKeyPath:keyPath ofObject:object change:change context:context]; } } // Swift override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) { if keyPath == #keyPath(AVPlayer.timeControlStatus) { // Deal w/ `change?[.newKey]` } else { super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context) } } Y el último paso más importante , recuerda eliminar el observador cuando ya no lo necesites, generalmente en -dealloc :
[playerViewController.player removeObserver:self forKeyPath:@"timeControlStatus"]; Por cierto, también puede observar la propiedad rate de AVPlayer, hacer que -play sea equivalente a establecer el valor de rate en 1.0 y -pause equivalente a establecer el valor de rate en 0.0.
Pero en su caso, creo que timeControlStatus tiene más sentido.
Hay un DOC oficial para leer más (pero solo los estados "Listo para jugar", "Error" y "Desconocido", inútil aquí): "Respondiendo a los cambios de estado de reproducción" .
Espero eso ayude.