2022-05-19 07:29:25 -06:00
|
|
|
import AVFoundation
|
|
|
|
import AVKit
|
|
|
|
import Foundation
|
|
|
|
|
2023-12-07 00:47:40 -07:00
|
|
|
// MARK: - RCTPlayerObserverHandlerObjc
|
|
|
|
|
2022-05-19 07:29:25 -06:00
|
|
|
@objc
|
|
|
|
protocol RCTPlayerObserverHandlerObjc {
|
2023-12-07 00:47:40 -07:00
|
|
|
func handleDidFailToFinishPlaying(notification: NSNotification!)
|
|
|
|
func handlePlaybackStalled(notification: NSNotification!)
|
|
|
|
func handlePlayerItemDidReachEnd(notification: NSNotification!)
|
|
|
|
func handleAVPlayerAccess(notification: NSNotification!)
|
2022-05-19 07:29:25 -06:00
|
|
|
}
|
|
|
|
|
2023-12-07 00:47:40 -07:00
|
|
|
// MARK: - RCTPlayerObserverHandler
|
|
|
|
|
2022-05-19 07:29:25 -06:00
|
|
|
protocol RCTPlayerObserverHandler: RCTPlayerObserverHandlerObjc {
|
2023-12-07 00:47:40 -07:00
|
|
|
func handleTimeUpdate(time: CMTime)
|
|
|
|
func handleReadyForDisplay(changeObject: Any, change: NSKeyValueObservedChange<Bool>)
|
2024-01-04 06:14:51 -07:00
|
|
|
func handleTimeMetadataChange(timedMetadata: [AVMetadataItem])
|
2023-12-07 00:47:40 -07:00
|
|
|
func handlePlayerItemStatusChange(playerItem: AVPlayerItem, change: NSKeyValueObservedChange<AVPlayerItem.Status>)
|
|
|
|
func handlePlaybackBufferKeyEmpty(playerItem: AVPlayerItem, change: NSKeyValueObservedChange<Bool>)
|
|
|
|
func handlePlaybackLikelyToKeepUp(playerItem: AVPlayerItem, change: NSKeyValueObservedChange<Bool>)
|
2022-05-19 07:29:25 -06:00
|
|
|
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>)
|
2023-12-07 00:47:40 -07:00
|
|
|
func handleViewControllerOverlayViewFrameChange(overlayView: UIView, change: NSKeyValueObservedChange<CGRect>)
|
2024-02-01 00:56:00 -07:00
|
|
|
func handleTracksChange(playerItem: AVPlayerItem, change: NSKeyValueObservedChange<[AVPlayerItemTrack]>)
|
2024-02-29 06:41:04 -07:00
|
|
|
func handleLegibleOutput(strings: [NSAttributedString])
|
2022-05-19 07:29:25 -06:00
|
|
|
}
|
|
|
|
|
2023-12-07 00:47:40 -07:00
|
|
|
// MARK: - RCTPlayerObserver
|
|
|
|
|
2024-02-29 06:41:04 -07:00
|
|
|
class RCTPlayerObserver: NSObject, AVPlayerItemMetadataOutputPushDelegate, AVPlayerItemLegibleOutputPushDelegate {
|
2023-07-06 01:52:33 -06:00
|
|
|
weak var _handlers: RCTPlayerObserverHandler?
|
2023-12-07 00:47:40 -07:00
|
|
|
|
|
|
|
var player: AVPlayer? {
|
2022-05-19 07:29:25 -06:00
|
|
|
willSet {
|
|
|
|
removePlayerObservers()
|
|
|
|
removePlayerTimeObserver()
|
|
|
|
}
|
|
|
|
didSet {
|
|
|
|
if player != nil {
|
|
|
|
addPlayerObservers()
|
|
|
|
addPlayerTimeObserver()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-12-07 00:47:40 -07:00
|
|
|
|
|
|
|
var playerItem: AVPlayerItem? {
|
2022-05-19 07:29:25 -06:00
|
|
|
willSet {
|
|
|
|
removePlayerItemObservers()
|
|
|
|
}
|
|
|
|
didSet {
|
2024-01-04 06:14:51 -07:00
|
|
|
guard let playerItem else { return }
|
|
|
|
|
|
|
|
addPlayerItemObservers()
|
|
|
|
|
|
|
|
// handle timedMetadata
|
|
|
|
let metadataOutput = AVPlayerItemMetadataOutput()
|
2024-02-29 06:41:04 -07:00
|
|
|
let legibleOutput = AVPlayerItemLegibleOutput()
|
2024-01-04 06:14:51 -07:00
|
|
|
playerItem.add(metadataOutput)
|
2024-02-29 06:41:04 -07:00
|
|
|
playerItem.add(legibleOutput)
|
2024-01-04 06:14:51 -07:00
|
|
|
metadataOutput.setDelegate(self, queue: .main)
|
2024-02-29 06:41:04 -07:00
|
|
|
legibleOutput.setDelegate(self, queue: .main)
|
2022-05-19 07:29:25 -06:00
|
|
|
}
|
|
|
|
}
|
2023-12-07 00:47:40 -07:00
|
|
|
|
|
|
|
var playerViewController: AVPlayerViewController? {
|
2022-05-19 07:29:25 -06:00
|
|
|
willSet {
|
|
|
|
removePlayerViewControllerObservers()
|
|
|
|
}
|
|
|
|
didSet {
|
|
|
|
if playerViewController != nil {
|
|
|
|
addPlayerViewControllerObservers()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-12-07 00:47:40 -07:00
|
|
|
|
|
|
|
var playerLayer: AVPlayerLayer? {
|
2022-05-19 07:29:25 -06:00
|
|
|
willSet {
|
|
|
|
removePlayerLayerObserver()
|
|
|
|
}
|
|
|
|
didSet {
|
2022-06-14 17:58:47 -06:00
|
|
|
if playerLayer != nil {
|
2022-05-19 07:29:25 -06:00
|
|
|
addPlayerLayerObserver()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-12-07 00:47:40 -07:00
|
|
|
|
|
|
|
private var _progressUpdateInterval: TimeInterval = 250
|
|
|
|
private var _timeObserver: Any?
|
|
|
|
|
|
|
|
private var _playerRateChangeObserver: NSKeyValueObservation?
|
|
|
|
private var _playerVolumeChangeObserver: NSKeyValueObservation?
|
|
|
|
private var _playerExternalPlaybackActiveObserver: NSKeyValueObservation?
|
|
|
|
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?
|
2024-02-01 00:56:00 -07:00
|
|
|
private var _playerTracksObserver: NSKeyValueObservation?
|
2023-12-07 00:47:40 -07:00
|
|
|
|
2022-05-19 07:29:25 -06:00
|
|
|
deinit {
|
2024-01-04 12:16:23 -07:00
|
|
|
if let _handlers {
|
2023-07-06 01:52:33 -06:00
|
|
|
NotificationCenter.default.removeObserver(_handlers)
|
|
|
|
}
|
2022-05-19 07:29:25 -06:00
|
|
|
}
|
2023-12-07 00:47:40 -07:00
|
|
|
|
2024-01-04 06:14:51 -07:00
|
|
|
func metadataOutput(_: AVPlayerItemMetadataOutput, didOutputTimedMetadataGroups groups: [AVTimedMetadataGroup], from _: AVPlayerItemTrack?) {
|
|
|
|
guard let _handlers else { return }
|
|
|
|
|
|
|
|
for metadataGroup in groups {
|
|
|
|
_handlers.handleTimeMetadataChange(timedMetadata: metadataGroup.items)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-29 06:41:04 -07:00
|
|
|
func legibleOutput(_: AVPlayerItemLegibleOutput,
|
|
|
|
didOutputAttributedStrings strings: [NSAttributedString],
|
|
|
|
nativeSampleBuffers _: [Any],
|
|
|
|
forItemTime _: CMTime) {
|
|
|
|
guard let _handlers else { return }
|
|
|
|
_handlers.handleLegibleOutput(strings: strings)
|
|
|
|
}
|
|
|
|
|
2022-05-19 07:29:25 -06:00
|
|
|
func addPlayerObservers() {
|
2024-01-04 06:14:51 -07:00
|
|
|
guard let player, let _handlers else {
|
2022-05-19 07:29:25 -06:00
|
|
|
return
|
|
|
|
}
|
2023-12-07 00:47:40 -07:00
|
|
|
|
2023-10-24 14:10:29 -06:00
|
|
|
_playerRateChangeObserver = player.observe(\.rate, options: [.old], changeHandler: _handlers.handlePlaybackRateChange)
|
2023-12-07 00:47:40 -07:00
|
|
|
_playerVolumeChangeObserver = player.observe(\.volume, options: [.old], changeHandler: _handlers.handleVolumeChange)
|
2024-01-15 00:04:29 -07:00
|
|
|
#if !os(visionOS)
|
|
|
|
_playerExternalPlaybackActiveObserver = player.observe(\.isExternalPlaybackActive, changeHandler: _handlers.handleExternalPlaybackActiveChange)
|
|
|
|
#endif
|
2022-05-19 07:29:25 -06:00
|
|
|
}
|
2023-12-07 00:47:40 -07:00
|
|
|
|
2022-05-19 07:29:25 -06:00
|
|
|
func removePlayerObservers() {
|
|
|
|
_playerRateChangeObserver?.invalidate()
|
2023-10-23 10:23:57 -06:00
|
|
|
_playerExternalPlaybackActiveObserver?.invalidate()
|
2024-03-06 15:55:44 -07:00
|
|
|
_playerVolumeChangeObserver?.invalidate()
|
2022-05-19 07:29:25 -06:00
|
|
|
}
|
2023-12-07 00:47:40 -07:00
|
|
|
|
2022-05-19 07:29:25 -06:00
|
|
|
func addPlayerItemObservers() {
|
2024-01-04 06:14:51 -07:00
|
|
|
guard let playerItem, let _handlers else { return }
|
2023-12-07 00:47:40 -07: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
|
|
|
|
)
|
2024-02-01 00:56:00 -07:00
|
|
|
|
|
|
|
// observe tracks update
|
|
|
|
_playerTracksObserver = playerItem.observe(
|
|
|
|
\.tracks,
|
|
|
|
options: [.new, .old],
|
|
|
|
changeHandler: _handlers.handleTracksChange
|
|
|
|
)
|
2022-05-19 07:29:25 -06:00
|
|
|
}
|
2023-12-07 00:47:40 -07:00
|
|
|
|
2022-05-19 07:29:25 -06:00
|
|
|
func removePlayerItemObservers() {
|
|
|
|
_playerItemStatusObserver?.invalidate()
|
|
|
|
_playerPlaybackBufferEmptyObserver?.invalidate()
|
|
|
|
_playerPlaybackLikelyToKeepUpObserver?.invalidate()
|
|
|
|
_playerTimedMetadataObserver?.invalidate()
|
2024-02-01 00:56:00 -07:00
|
|
|
_playerTracksObserver?.invalidate()
|
2022-05-19 07:29:25 -06:00
|
|
|
}
|
2023-12-07 00:47:40 -07:00
|
|
|
|
2022-05-19 07:29:25 -06:00
|
|
|
func addPlayerViewControllerObservers() {
|
2024-01-04 06:14:51 -07:00
|
|
|
guard let playerViewController, let _handlers else { return }
|
2023-12-07 00:47:40 -07:00
|
|
|
|
2024-01-15 00:04:29 -07:00
|
|
|
#if !os(visionOS)
|
|
|
|
_playerViewControllerReadyForDisplayObserver = playerViewController.observe(
|
|
|
|
\.isReadyForDisplay,
|
|
|
|
options: [.new],
|
|
|
|
changeHandler: _handlers.handleReadyForDisplay
|
|
|
|
)
|
|
|
|
#endif
|
2023-12-07 00:47:40 -07:00
|
|
|
|
|
|
|
_playerViewControllerOverlayFrameObserver = playerViewController.contentOverlayView?.observe(
|
|
|
|
\.frame,
|
|
|
|
options: [.new, .old],
|
|
|
|
changeHandler: _handlers.handleViewControllerOverlayViewFrameChange
|
|
|
|
)
|
2022-05-19 07:29:25 -06:00
|
|
|
}
|
2023-12-07 00:47:40 -07:00
|
|
|
|
2022-05-19 07:29:25 -06:00
|
|
|
func removePlayerViewControllerObservers() {
|
|
|
|
_playerViewControllerReadyForDisplayObserver?.invalidate()
|
|
|
|
_playerViewControllerOverlayFrameObserver?.invalidate()
|
|
|
|
}
|
2023-12-07 00:47:40 -07:00
|
|
|
|
2022-05-19 07:29:25 -06:00
|
|
|
func addPlayerLayerObserver() {
|
2024-01-04 06:14:51 -07:00
|
|
|
guard let _handlers else { return }
|
2023-12-07 00:47:40 -07:00
|
|
|
_playerLayerReadyForDisplayObserver = playerLayer?.observe(\.isReadyForDisplay, options: [.new], changeHandler: _handlers.handleReadyForDisplay)
|
2022-05-19 07:29:25 -06:00
|
|
|
}
|
2023-12-07 00:47:40 -07:00
|
|
|
|
2022-05-19 07:29:25 -06:00
|
|
|
func removePlayerLayerObserver() {
|
|
|
|
_playerLayerReadyForDisplayObserver?.invalidate()
|
|
|
|
}
|
2023-12-07 00:47:40 -07:00
|
|
|
|
2022-05-19 07:29:25 -06:00
|
|
|
func addPlayerTimeObserver() {
|
2024-01-04 06:14:51 -07:00
|
|
|
guard let _handlers else { return }
|
2022-05-19 07:29:25 -06:00
|
|
|
removePlayerTimeObserver()
|
2023-12-07 00:47:40 -07:00
|
|
|
let progressUpdateIntervalMS: Float64 = _progressUpdateInterval / 1000
|
2022-05-19 07:29:25 -06:00
|
|
|
// @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)),
|
2023-12-07 00:47:40 -07:00
|
|
|
queue: nil,
|
|
|
|
using: _handlers.handleTimeUpdate
|
2022-05-19 07:29:25 -06:00
|
|
|
)
|
|
|
|
}
|
2023-12-07 00:47:40 -07:00
|
|
|
|
2022-05-19 07:29:25 -06:00
|
|
|
/* Cancels the previously registered time observer. */
|
|
|
|
func removePlayerTimeObserver() {
|
|
|
|
if _timeObserver != nil {
|
|
|
|
player?.removeTimeObserver(_timeObserver)
|
|
|
|
_timeObserver = nil
|
|
|
|
}
|
|
|
|
}
|
2023-12-07 00:47:40 -07:00
|
|
|
|
2022-05-19 07:29:25 -06:00
|
|
|
func addTimeObserverIfNotSet() {
|
2023-12-07 00:47:40 -07:00
|
|
|
if _timeObserver == nil {
|
2022-05-19 07:29:25 -06:00
|
|
|
addPlayerTimeObserver()
|
|
|
|
}
|
|
|
|
}
|
2023-12-07 00:47:40 -07:00
|
|
|
|
|
|
|
func replaceTimeObserverIfSet(_ newUpdateInterval: Float64? = nil) {
|
2024-01-04 12:16:23 -07:00
|
|
|
if let newUpdateInterval {
|
2022-05-19 07:29:25 -06:00
|
|
|
_progressUpdateInterval = newUpdateInterval
|
|
|
|
}
|
2023-12-07 00:47:40 -07:00
|
|
|
if _timeObserver != nil {
|
2022-05-19 07:29:25 -06:00
|
|
|
addPlayerTimeObserver()
|
|
|
|
}
|
|
|
|
}
|
2023-12-07 00:47:40 -07:00
|
|
|
|
2022-05-19 07:29:25 -06:00
|
|
|
func attachPlayerEventListeners() {
|
2024-01-04 06:14:51 -07:00
|
|
|
guard let _handlers else { return }
|
2022-05-19 07:29:25 -06:00
|
|
|
NotificationCenter.default.removeObserver(_handlers,
|
2023-12-07 00:47:40 -07:00
|
|
|
name: NSNotification.Name.AVPlayerItemDidPlayToEndTime,
|
|
|
|
object: player?.currentItem)
|
|
|
|
|
2022-05-19 07:29:25 -06:00
|
|
|
NotificationCenter.default.addObserver(_handlers,
|
2023-12-07 00:47:40 -07:00
|
|
|
selector: #selector(RCTPlayerObserverHandler.handlePlayerItemDidReachEnd(notification:)),
|
|
|
|
name: NSNotification.Name.AVPlayerItemDidPlayToEndTime,
|
|
|
|
object: player?.currentItem)
|
|
|
|
|
2022-05-19 07:29:25 -06:00
|
|
|
NotificationCenter.default.removeObserver(_handlers,
|
2023-12-07 00:47:40 -07:00
|
|
|
name: NSNotification.Name.AVPlayerItemPlaybackStalled,
|
|
|
|
object: nil)
|
|
|
|
|
2022-05-19 07:29:25 -06:00
|
|
|
NotificationCenter.default.addObserver(_handlers,
|
2023-12-07 00:47:40 -07:00
|
|
|
selector: #selector(RCTPlayerObserverHandler.handlePlaybackStalled(notification:)),
|
|
|
|
name: NSNotification.Name.AVPlayerItemPlaybackStalled,
|
|
|
|
object: nil)
|
|
|
|
|
2022-05-19 07:29:25 -06:00
|
|
|
NotificationCenter.default.removeObserver(_handlers,
|
|
|
|
name: NSNotification.Name.AVPlayerItemFailedToPlayToEndTime,
|
2023-12-07 00:47:40 -07:00
|
|
|
object: nil)
|
|
|
|
|
2022-05-19 07:29:25 -06:00
|
|
|
NotificationCenter.default.addObserver(_handlers,
|
2023-12-07 00:47:40 -07:00
|
|
|
selector: #selector(RCTPlayerObserverHandler.handleDidFailToFinishPlaying(notification:)),
|
2022-05-19 07:29:25 -06:00
|
|
|
name: NSNotification.Name.AVPlayerItemFailedToPlayToEndTime,
|
2023-12-07 00:47:40 -07:00
|
|
|
object: nil)
|
|
|
|
|
2023-11-08 14:06:29 -07:00
|
|
|
NotificationCenter.default.removeObserver(_handlers, name: NSNotification.Name.AVPlayerItemNewAccessLogEntry, object: player?.currentItem)
|
2023-12-07 00:47:40 -07:00
|
|
|
|
2023-11-08 14:06:29 -07:00
|
|
|
NotificationCenter.default.addObserver(_handlers,
|
2023-12-07 00:47:40 -07:00
|
|
|
selector: #selector(RCTPlayerObserverHandlerObjc.handleAVPlayerAccess(notification:)),
|
2023-11-08 14:06:29 -07:00
|
|
|
name: NSNotification.Name.AVPlayerItemNewAccessLogEntry,
|
|
|
|
object: player?.currentItem)
|
2022-05-19 07:29:25 -06:00
|
|
|
}
|
2023-12-07 00:47:40 -07:00
|
|
|
|
2022-05-19 07:29:25 -06:00
|
|
|
func clearPlayer() {
|
|
|
|
player = nil
|
|
|
|
playerItem = nil
|
2024-01-04 06:14:51 -07:00
|
|
|
if let _handlers {
|
2023-07-06 01:52:33 -06:00
|
|
|
NotificationCenter.default.removeObserver(_handlers)
|
|
|
|
}
|
2022-05-19 07:29:25 -06:00
|
|
|
}
|
|
|
|
}
|