From 08a57a3ba3c0de55d08684036fb6eb157995419c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C5=82a=C5=BCej=20Lewandowski?= Date: Fri, 2 Aug 2024 10:49:52 +0200 Subject: [PATCH] fix(ios): metadata update race (#4033) * fix metadata update race * merge nowPlayingInfo instead of overriding it * update method name * fix code style --- ios/Video/NowPlayingInfoCenterManager.swift | 35 +++++---------------- ios/Video/RCTVideo.swift | 4 +-- 2 files changed, 9 insertions(+), 30 deletions(-) diff --git a/ios/Video/NowPlayingInfoCenterManager.swift b/ios/Video/NowPlayingInfoCenterManager.swift index 36bfe8eb..981f8ab5 100644 --- a/ios/Video/NowPlayingInfoCenterManager.swift +++ b/ios/Video/NowPlayingInfoCenterManager.swift @@ -72,7 +72,7 @@ class NowPlayingInfoCenterManager { if currentPlayer == player { currentPlayer = nil - updateMetadata() + updateNowPlayingInfo() } if players.allObjects.isEmpty { @@ -106,14 +106,12 @@ class NowPlayingInfoCenterManager { currentPlayer = player registerCommandTargets() - updateMetadata() - - // one second observer + updateNowPlayingInfo() playbackObserver = player.addPeriodicTimeObserver( forInterval: CMTime(value: 1, timescale: 4), queue: .global(), using: { [weak self] _ in - self?.updatePlaybackInfo() + self?.updateNowPlayingInfo() } ) } @@ -186,7 +184,7 @@ class NowPlayingInfoCenterManager { remoteCommandCenter.changePlaybackPositionCommand.removeTarget(playbackPositionTarget) } - public func updateMetadata() { + public func updateNowPlayingInfo() { guard let player = currentPlayer, let currentItem = player.currentItem else { invalidateCommandTargets() MPNowPlayingInfoCenter.default().nowPlayingInfo = [:] @@ -206,7 +204,7 @@ class NowPlayingInfoCenterManager { let image = imgData.flatMap { UIImage(data: $0) } ?? UIImage() let artworkItem = MPMediaItemArtwork(boundsSize: image.size) { _ in image } - let nowPlayingInfo: [String: Any] = [ + let newNowPlayingInfo: [String: Any] = [ MPMediaItemPropertyTitle: titleItem, MPMediaItemPropertyArtist: artistItem, MPMediaItemPropertyArtwork: artworkItem, @@ -215,28 +213,9 @@ class NowPlayingInfoCenterManager { MPNowPlayingInfoPropertyPlaybackRate: player.rate, MPNowPlayingInfoPropertyIsLiveStream: CMTIME_IS_INDEFINITE(currentItem.asset.duration), ] + let currentNowPlayingInfo = MPNowPlayingInfoCenter.default().nowPlayingInfo ?? [:] - MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo - } - - private func updatePlaybackInfo() { - guard let player = currentPlayer, let currentItem = player.currentItem else { - return - } - - // We dont want to update playback if we did not set metadata yet - if var nowPlayingInfo = MPNowPlayingInfoCenter.default().nowPlayingInfo { - let newNowPlayingInfo: [String: Any] = [ - MPMediaItemPropertyPlaybackDuration: currentItem.duration.seconds, - MPNowPlayingInfoPropertyElapsedPlaybackTime: currentItem.currentTime().seconds.rounded(), - MPNowPlayingInfoPropertyPlaybackRate: player.rate, - MPNowPlayingInfoPropertyIsLiveStream: CMTIME_IS_INDEFINITE(currentItem.asset.duration), - ] - - nowPlayingInfo.merge(newNowPlayingInfo) { _, v in v } - - MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo - } + MPNowPlayingInfoCenter.default().nowPlayingInfo = currentNowPlayingInfo.merging(newNowPlayingInfo) { _, new in new } } private func findNewCurrentPlayer() { diff --git a/ios/Video/RCTVideo.swift b/ios/Video/RCTVideo.swift index c4f4e99a..325a06f1 100644 --- a/ios/Video/RCTVideo.swift +++ b/ios/Video/RCTVideo.swift @@ -490,8 +490,8 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH } else { _player?.replaceCurrentItem(with: playerItem) - // later we can just call "updateMetadata: - NowPlayingInfoCenterManager.shared.updateMetadata() + // later we can just call "updateNowPlayingInfo: + NowPlayingInfoCenterManager.shared.updateNowPlayingInfo() } _playerObserver.player = _player