2022-05-19 07:29:25 -06:00
|
|
|
import AVFoundation
|
|
|
|
import AVKit
|
|
|
|
import Foundation
|
|
|
|
|
|
|
|
@objc
|
|
|
|
protocol RCTPlayerObserverHandlerObjc {
|
|
|
|
func handleDidFailToFinishPlaying(notification:NSNotification!)
|
|
|
|
func handlePlaybackStalled(notification:NSNotification!)
|
|
|
|
func handlePlayerItemDidReachEnd(notification:NSNotification!)
|
|
|
|
// unused
|
|
|
|
// func handleAVPlayerAccess(notification:NSNotification!)
|
|
|
|
}
|
|
|
|
|
|
|
|
protocol RCTPlayerObserverHandler: RCTPlayerObserverHandlerObjc {
|
|
|
|
func handleTimeUpdate(time:CMTime)
|
|
|
|
func handleReadyForDisplay(changeObject: Any, change:NSKeyValueObservedChange<Bool>)
|
|
|
|
func handleTimeMetadataChange(playerItem:AVPlayerItem, change:NSKeyValueObservedChange<[AVMetadataItem]?>)
|
|
|
|
func handlePlayerItemStatusChange(playerItem:AVPlayerItem, change:NSKeyValueObservedChange<AVPlayerItem.Status>)
|
|
|
|
func handlePlaybackBufferKeyEmpty(playerItem:AVPlayerItem, change:NSKeyValueObservedChange<Bool>)
|
|
|
|
func handlePlaybackLikelyToKeepUp(playerItem:AVPlayerItem, change:NSKeyValueObservedChange<Bool>)
|
|
|
|
func handlePlaybackRateChange(player: AVPlayer, change: NSKeyValueObservedChange<Float>)
|
2023-11-04 11:11:54 -06:00
|
|
|
func handleVolumeChange(player: AVPlayer, change: NSKeyValueObservedChange<Float>)
|
2022-05-19 07:29:25 -06:00
|
|
|
func handleExternalPlaybackActiveChange(player: AVPlayer, change: NSKeyValueObservedChange<Bool>)
|
|
|
|
func handleViewControllerOverlayViewFrameChange(overlayView:UIView, change:NSKeyValueObservedChange<CGRect>)
|
|
|
|
}
|
|
|
|
|
|
|
|
class RCTPlayerObserver: NSObject {
|
|
|
|
|
2023-07-06 01:52:33 -06:00
|
|
|
weak var _handlers: RCTPlayerObserverHandler?
|
2022-05-19 07:29:25 -06:00
|
|
|
|
|
|
|
var player:AVPlayer? {
|
|
|
|
willSet {
|
|
|
|
removePlayerObservers()
|
|
|
|
removePlayerTimeObserver()
|
|
|
|
}
|
|
|
|
didSet {
|
|
|
|
if player != nil {
|
|
|
|
addPlayerObservers()
|
|
|
|
addPlayerTimeObserver()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var playerItem:AVPlayerItem? {
|
|
|
|
willSet {
|
|
|
|
removePlayerItemObservers()
|
|
|
|
}
|
|
|
|
didSet {
|
|
|
|
if playerItem != nil {
|
|
|
|
addPlayerItemObservers()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var playerViewController:AVPlayerViewController? {
|
|
|
|
willSet {
|
|
|
|
removePlayerViewControllerObservers()
|
|
|
|
}
|
|
|
|
didSet {
|
|
|
|
if playerViewController != nil {
|
|
|
|
addPlayerViewControllerObservers()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var playerLayer:AVPlayerLayer? {
|
|
|
|
willSet {
|
|
|
|
removePlayerLayerObserver()
|
|
|
|
}
|
|
|
|
didSet {
|
2022-06-14 17:58:47 -06:00
|
|
|
if playerLayer != nil {
|
2022-05-19 07:29:25 -06:00
|
|
|
addPlayerLayerObserver()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private var _progressUpdateInterval:TimeInterval = 250
|
|
|
|
private var _timeObserver:Any?
|
|
|
|
|
|
|
|
private var _playerRateChangeObserver:NSKeyValueObservation?
|
2023-11-04 11:11:54 -06:00
|
|
|
private var _playerVolumeChangeObserver:NSKeyValueObservation?
|
2023-10-23 10:23:57 -06:00
|
|
|
private var _playerExternalPlaybackActiveObserver:NSKeyValueObservation?
|
2022-05-19 07:29:25 -06:00
|
|
|
private var _playerItemStatusObserver:NSKeyValueObservation?
|
|
|
|
private var _playerPlaybackBufferEmptyObserver:NSKeyValueObservation?
|
|
|
|
private var _playerPlaybackLikelyToKeepUpObserver:NSKeyValueObservation?
|
|
|
|
private var _playerTimedMetadataObserver:NSKeyValueObservation?
|
|
|
|
private var _playerViewControllerReadyForDisplayObserver:NSKeyValueObservation?
|
|
|
|
private var _playerLayerReadyForDisplayObserver:NSKeyValueObservation?
|
|
|
|
private var _playerViewControllerOverlayFrameObserver:NSKeyValueObservation?
|
|
|
|
|
|
|
|
deinit {
|
2023-07-06 01:52:33 -06:00
|
|
|
if let _handlers = _handlers {
|
|
|
|
NotificationCenter.default.removeObserver(_handlers)
|
|
|
|
}
|
2022-05-19 07:29:25 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func addPlayerObservers() {
|
2023-07-06 01:52:33 -06:00
|
|
|
guard let player = player, let _handlers = _handlers else {
|
2022-05-19 07:29:25 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-10-24 14:10:29 -06:00
|
|
|
_playerRateChangeObserver = player.observe(\.rate, options: [.old], changeHandler: _handlers.handlePlaybackRateChange)
|
2023-11-04 11:11:54 -06:00
|
|
|
_playerVolumeChangeObserver = player.observe(\.volume, options: [.old] ,changeHandler: _handlers.handleVolumeChange)
|
2023-10-23 10:23:57 -06:00
|
|
|
_playerExternalPlaybackActiveObserver = player.observe(\.isExternalPlaybackActive, changeHandler: _handlers.handleExternalPlaybackActiveChange)
|
2022-05-19 07:29:25 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func removePlayerObservers() {
|
|
|
|
_playerRateChangeObserver?.invalidate()
|
2023-10-23 10:23:57 -06:00
|
|
|
_playerExternalPlaybackActiveObserver?.invalidate()
|
2022-05-19 07:29:25 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func addPlayerItemObservers() {
|
2023-07-06 01:52:33 -06:00
|
|
|
guard let playerItem = playerItem, let _handlers = _handlers else { return }
|
2022-05-19 07:29:25 -06:00
|
|
|
|
|
|
|
_playerItemStatusObserver = playerItem.observe(\.status, options: [.new, .old], changeHandler: _handlers.handlePlayerItemStatusChange)
|
|
|
|
_playerPlaybackBufferEmptyObserver = playerItem.observe(\.isPlaybackBufferEmpty, options: [.new, .old], changeHandler: _handlers.handlePlaybackBufferKeyEmpty)
|
|
|
|
_playerPlaybackLikelyToKeepUpObserver = playerItem.observe(\.isPlaybackLikelyToKeepUp, options: [.new, .old], changeHandler: _handlers.handlePlaybackLikelyToKeepUp)
|
|
|
|
_playerTimedMetadataObserver = playerItem.observe(\.timedMetadata, options: [.new], changeHandler: _handlers.handleTimeMetadataChange)
|
|
|
|
}
|
|
|
|
|
|
|
|
func removePlayerItemObservers() {
|
|
|
|
_playerItemStatusObserver?.invalidate()
|
|
|
|
_playerPlaybackBufferEmptyObserver?.invalidate()
|
|
|
|
_playerPlaybackLikelyToKeepUpObserver?.invalidate()
|
|
|
|
_playerTimedMetadataObserver?.invalidate()
|
|
|
|
}
|
|
|
|
|
|
|
|
func addPlayerViewControllerObservers() {
|
2023-07-06 01:52:33 -06:00
|
|
|
guard let playerViewController = playerViewController, let _handlers = _handlers else { return }
|
2022-05-19 07:29:25 -06:00
|
|
|
|
|
|
|
_playerViewControllerReadyForDisplayObserver = playerViewController.observe(\.isReadyForDisplay, options: [.new], changeHandler: _handlers.handleReadyForDisplay)
|
|
|
|
|
|
|
|
_playerViewControllerOverlayFrameObserver = playerViewController.contentOverlayView?.observe(\.frame, options: [.new, .old], changeHandler: _handlers.handleViewControllerOverlayViewFrameChange)
|
|
|
|
}
|
|
|
|
|
|
|
|
func removePlayerViewControllerObservers() {
|
|
|
|
_playerViewControllerReadyForDisplayObserver?.invalidate()
|
|
|
|
_playerViewControllerOverlayFrameObserver?.invalidate()
|
|
|
|
}
|
|
|
|
|
|
|
|
func addPlayerLayerObserver() {
|
2023-07-06 01:52:33 -06:00
|
|
|
guard let _handlers = _handlers else {return}
|
2022-05-19 07:29:25 -06:00
|
|
|
_playerLayerReadyForDisplayObserver = playerLayer?.observe(\.isReadyForDisplay, options: [.new], changeHandler: _handlers.handleReadyForDisplay)
|
|
|
|
}
|
|
|
|
|
|
|
|
func removePlayerLayerObserver() {
|
|
|
|
_playerLayerReadyForDisplayObserver?.invalidate()
|
|
|
|
}
|
|
|
|
|
|
|
|
func addPlayerTimeObserver() {
|
2023-07-06 01:52:33 -06:00
|
|
|
guard let _handlers = _handlers else {return}
|
2022-05-19 07:29:25 -06:00
|
|
|
removePlayerTimeObserver()
|
|
|
|
let progressUpdateIntervalMS:Float64 = _progressUpdateInterval / 1000
|
|
|
|
// @see endScrubbing in AVPlayerDemoPlaybackViewController.m
|
|
|
|
// of https://developer.apple.com/library/ios/samplecode/AVPlayerDemo/Introduction/Intro.html
|
|
|
|
_timeObserver = player?.addPeriodicTimeObserver(
|
|
|
|
forInterval: CMTimeMakeWithSeconds(progressUpdateIntervalMS, preferredTimescale: Int32(NSEC_PER_SEC)),
|
|
|
|
queue:nil,
|
|
|
|
using:_handlers.handleTimeUpdate
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Cancels the previously registered time observer. */
|
|
|
|
func removePlayerTimeObserver() {
|
|
|
|
if _timeObserver != nil {
|
|
|
|
player?.removeTimeObserver(_timeObserver)
|
|
|
|
_timeObserver = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func addTimeObserverIfNotSet() {
|
|
|
|
if (_timeObserver == nil) {
|
|
|
|
addPlayerTimeObserver()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func replaceTimeObserverIfSet(_ newUpdateInterval:Float64? = nil) {
|
|
|
|
if let newUpdateInterval = newUpdateInterval {
|
|
|
|
_progressUpdateInterval = newUpdateInterval
|
|
|
|
}
|
|
|
|
if (_timeObserver != nil) {
|
|
|
|
addPlayerTimeObserver()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func attachPlayerEventListeners() {
|
2023-07-06 01:52:33 -06:00
|
|
|
guard let _handlers = _handlers else {return}
|
2022-05-19 07:29:25 -06:00
|
|
|
|
|
|
|
NotificationCenter.default.removeObserver(_handlers,
|
|
|
|
name:NSNotification.Name.AVPlayerItemDidPlayToEndTime,
|
|
|
|
object:player?.currentItem)
|
|
|
|
NotificationCenter.default.addObserver(_handlers,
|
|
|
|
selector:#selector(RCTPlayerObserverHandler.handlePlayerItemDidReachEnd(notification:)),
|
|
|
|
name:NSNotification.Name.AVPlayerItemDidPlayToEndTime,
|
|
|
|
object:player?.currentItem)
|
|
|
|
|
|
|
|
NotificationCenter.default.removeObserver(_handlers,
|
|
|
|
name:NSNotification.Name.AVPlayerItemPlaybackStalled,
|
|
|
|
object:nil)
|
|
|
|
NotificationCenter.default.addObserver(_handlers,
|
|
|
|
selector:#selector(RCTPlayerObserverHandler.handlePlaybackStalled(notification:)),
|
|
|
|
name:NSNotification.Name.AVPlayerItemPlaybackStalled,
|
|
|
|
object:nil)
|
|
|
|
NotificationCenter.default.removeObserver(_handlers,
|
|
|
|
name: NSNotification.Name.AVPlayerItemFailedToPlayToEndTime,
|
|
|
|
object:nil)
|
|
|
|
NotificationCenter.default.addObserver(_handlers,
|
|
|
|
selector:#selector(RCTPlayerObserverHandler.handleDidFailToFinishPlaying(notification:)),
|
|
|
|
name: NSNotification.Name.AVPlayerItemFailedToPlayToEndTime,
|
|
|
|
object:nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func clearPlayer() {
|
|
|
|
player = nil
|
|
|
|
playerItem = nil
|
2023-07-06 01:52:33 -06:00
|
|
|
if let _handlers = _handlers {
|
|
|
|
NotificationCenter.default.removeObserver(_handlers)
|
|
|
|
}
|
2022-05-19 07:29:25 -06:00
|
|
|
}
|
|
|
|
}
|