fix(ios): update onPlaybackStateChanged implementation (#3687)

This commit is contained in:
YangJH 2024-04-18 17:11:15 +09:00 committed by GitHub
parent 1af12f9dfb
commit 042e13c1dc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 19 additions and 3 deletions

View File

@ -22,6 +22,7 @@ protocol RCTPlayerObserverHandler: RCTPlayerObserverHandlerObjc {
func handlePlaybackBufferKeyEmpty(playerItem: AVPlayerItem, change: NSKeyValueObservedChange<Bool>)
func handlePlaybackLikelyToKeepUp(playerItem: AVPlayerItem, change: NSKeyValueObservedChange<Bool>)
func handlePlaybackRateChange(player: AVPlayer, change: NSKeyValueObservedChange<Float>)
func handleTimeControlStatusChange(player: AVPlayer, change: NSKeyValueObservedChange<AVPlayer.TimeControlStatus>)
func handleVolumeChange(player: AVPlayer, change: NSKeyValueObservedChange<Float>)
func handleExternalPlaybackActiveChange(player: AVPlayer, change: NSKeyValueObservedChange<Bool>)
func handleViewControllerOverlayViewFrameChange(overlayView: UIView, change: NSKeyValueObservedChange<CGRect>)
@ -99,6 +100,7 @@ class RCTPlayerObserver: NSObject, AVPlayerItemMetadataOutputPushDelegate, AVPla
private var _playerRateChangeObserver: NSKeyValueObservation?
private var _playerVolumeChangeObserver: NSKeyValueObservation?
private var _playerTimeControlStatusChangeObserver: NSKeyValueObservation?
private var _playerExternalPlaybackActiveObserver: NSKeyValueObservation?
private var _playerItemStatusObserver: NSKeyValueObservation?
private var _playerPlaybackBufferEmptyObserver: NSKeyValueObservation?
@ -139,6 +141,7 @@ class RCTPlayerObserver: NSObject, AVPlayerItemMetadataOutputPushDelegate, AVPla
_playerRateChangeObserver = player.observe(\.rate, options: [.old], changeHandler: _handlers.handlePlaybackRateChange)
_playerVolumeChangeObserver = player.observe(\.volume, options: [.old], changeHandler: _handlers.handleVolumeChange)
_playerTimeControlStatusChangeObserver = player.observe(\.timeControlStatus, options: [.old], changeHandler: _handlers.handleTimeControlStatusChange)
#if !os(visionOS)
_playerExternalPlaybackActiveObserver = player.observe(\.isExternalPlaybackActive, changeHandler: _handlers.handleExternalPlaybackActiveChange)
#endif
@ -148,6 +151,7 @@ class RCTPlayerObserver: NSObject, AVPlayerItemMetadataOutputPushDelegate, AVPla
_playerRateChangeObserver?.invalidate()
_playerExternalPlaybackActiveObserver?.invalidate()
_playerVolumeChangeObserver?.invalidate()
_playerTimeControlStatusChangeObserver?.invalidate()
}
func addPlayerItemObservers() {

View File

@ -44,6 +44,7 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH
private var _muted = false
private var _paused = false
private var _repeat = false
private var _isPlaying: Bool?
private var _allowsExternalPlayback = true
private var _textTracks: [TextTrack]?
private var _selectedTextTrackCriteria: SelectedTrackCriteria?
@ -1378,6 +1379,20 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH
onVideoBuffer?(["isBuffering": false, "target": reactTag as Any])
}
func handleTimeControlStatusChange(player: AVPlayer, change: NSKeyValueObservedChange<AVPlayer.TimeControlStatus>) {
if player.timeControlStatus == change.oldValue && change.oldValue != nil {
return
}
guard [.paused, .playing].contains(player.timeControlStatus) else {
return
}
let isPlaying = player.timeControlStatus == .playing
guard _isPlaying == nil || _isPlaying! != isPlaying else { return }
_isPlaying = isPlaying
onVideoPlaybackStateChanged?(["isPlaying": isPlaying, "target": reactTag as Any])
}
func handlePlaybackRateChange(player: AVPlayer, change: NSKeyValueObservedChange<Float>) {
guard let _player else { return }
@ -1388,9 +1403,6 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH
onPlaybackRateChange?(["playbackRate": NSNumber(value: _player.rate),
"target": reactTag as Any])
onVideoPlaybackStateChanged?(["isPlaying": _player.rate != 0,
"target": reactTag as Any])
if _playbackStalled && _player.rate > 0 {
onPlaybackResume?(["playbackRate": NSNumber(value: _player.rate),
"target": reactTag as Any])