Reproduzir / Pausar e Tempo decorrido não atualizando corretamente no centro de comando do iOS

Eu tenho um player de vídeo que pode ser reproduzido no centro de comando do iOS e na tela de bloqueio. Quando alterno um botão de reprodução / pausa no meu aplicativo, ele deve atualizar o botão de reprodução / pausa no centro de comando (MPRemoteCommandCenter) atualizando onowPlayingInfo (MPNowPlayingInfoCenter) Não sei por que não está atualizando.

Por exemplo, se eu pausar o vídeo com um botão personalizado no meu aplicativo, o centro de comando ainda mostrará o botão de pausa (o que significa que o vídeo ainda está sendo reproduzido, o que está errado).

É assim que eu atualizo onowPlayingInfo:

func updateMPNowPlayingInforCenterMetadata() {
    guard video != nil else {
        nowPlayingInfoCenter.nowPlayingInfo = nil
        return
    }

    var nowPlayingInfo = nowPlayingInfoCenter.nowPlayingInfo ?? [String: Any]()

    let image: UIImage
    if let placeholderLocalURL = video.placeholderLocalURL, let placeholderImage = UIImage(contentsOfFile: placeholderLocalURL.path) {
        image = placeholderImage
    } else {
        image = UIImage()
    }

    let artwork = MPMediaItemArtwork(boundsSize: image.size, requestHandler: { _ -> UIImage in
        return image
    })

    nowPlayingInfo[MPMediaItemPropertyTitle] = video.title
    nowPlayingInfo[MPMediaItemPropertyAlbumTitle] = video.creator?.name ?? " "
    nowPlayingInfo[MPMediaItemPropertyArtwork] = artwork

    nowPlayingInfo[MPMediaItemPropertyPlaybackDuration] = Float(video.duration)
    nowPlayingInfo[MPNowPlayingInfoPropertyElapsedPlaybackTime] = Float(currentTime) // CMTimeGetSeconds(player.currentItem!.currentTime())
    nowPlayingInfo[MPNowPlayingInfoPropertyPlaybackRate] = player.rate
    nowPlayingInfo[MPNowPlayingInfoPropertyDefaultPlaybackRate] = player.rate

    nowPlayingInfoCenter.nowPlayingInfo = nowPlayingInfo

    if player.rate == 0.0 {
        state = .paused
    } else {
        state = .playing
    }
}

Com o KVO, quando o player estiverrate mudanças, eu chamo esta função:

// MARK: - Key-Value Observing Method

override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
    guard context == &assetPlaybackManagerKVOContext else {
        super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
        return
    }

    } else if keyPath == #keyPath(AVPlayer.rate) {
        updateMPNowPlayingInforCenterMetadata()
    }
}

Alguma ideia?

ATUALIZAR

Eu encontrei uma solução, mas não perfeita no meu caso. Então, no meu aplicativo, tenho 2 view controller. Vamos chamá-losFeedVC ePlayerVC. assimFeedVC temAVPlayersempre tocando, mas sem som. Se você clicar em um deles, oPlayerVC é criado e reproduz o vídeo completo.Se eu pausar oAVPlayer'pecadoFeedVC antes de ir paraPlayerVC então o botão "reproduzir / pausar" no NowPlayingInfoCenter funciona perfeitamente!

Existe uma maneira de fazer isso funcionar sem precisar pausar os vídeos noFeedVC?

Outra questão é que oelapsed time continua contando se eu não pausar os jogadores noFeedVC. Parece que, se vários jogadores estiverem jogando, o botão reproduzir / pausar e o tempo decorrido estão incorretos.

questionAnswers(1)

yourAnswerToTheQuestion