2021-10-06 01:50:09 +05:30
|
|
|
|
import AVKit
|
|
|
|
|
import Defaults
|
2021-06-14 23:35:02 +05:30
|
|
|
|
import Foundation
|
2021-06-15 22:05:21 +05:30
|
|
|
|
import Logging
|
2021-10-29 01:48:23 +05:30
|
|
|
|
import MediaPlayer
|
2021-07-12 02:22:49 +05:30
|
|
|
|
#if !os(macOS)
|
|
|
|
|
import UIKit
|
|
|
|
|
#endif
|
2021-10-17 04:18:58 +05:30
|
|
|
|
import Siesta
|
2021-10-23 22:19:45 +05:30
|
|
|
|
import SwiftUI
|
2021-10-17 04:18:58 +05:30
|
|
|
|
import SwiftyJSON
|
2021-06-14 23:35:02 +05:30
|
|
|
|
|
2021-09-25 13:48:22 +05:30
|
|
|
|
final class PlayerModel: ObservableObject {
|
2021-11-02 22:54:59 +05:30
|
|
|
|
static let availableRates: [Float] = [0.5, 0.67, 0.8, 1, 1.25, 1.5, 2]
|
2021-11-07 19:02:01 +05:30
|
|
|
|
let logger = Logger(label: "stream.yattee.app")
|
2021-06-15 22:05:21 +05:30
|
|
|
|
|
2021-10-06 01:50:09 +05:30
|
|
|
|
private(set) var player = AVPlayer()
|
2021-10-28 22:44:55 +05:30
|
|
|
|
private(set) var playerView = Player()
|
|
|
|
|
var controller: PlayerViewController? { didSet { playerView.controller = controller } }
|
2021-10-06 01:50:09 +05:30
|
|
|
|
#if os(tvOS)
|
|
|
|
|
var avPlayerViewController: AVPlayerViewController?
|
|
|
|
|
#endif
|
2021-06-16 02:51:57 +05:30
|
|
|
|
|
2021-11-05 20:28:51 +05:30
|
|
|
|
@Published var presentingPlayer = false { didSet { pauseOnPlayerDismiss() } }
|
2021-06-18 15:47:01 +05:30
|
|
|
|
|
2021-10-06 01:50:09 +05:30
|
|
|
|
@Published var stream: Stream?
|
2021-11-02 22:54:59 +05:30
|
|
|
|
@Published var currentRate: Float = 1.0 { didSet { player.rate = currentRate } }
|
2021-06-15 22:05:21 +05:30
|
|
|
|
|
2021-10-23 22:19:45 +05:30
|
|
|
|
@Published var availableStreams = [Stream]() { didSet { rebuildTVMenu() } }
|
|
|
|
|
@Published var streamSelection: Stream? { didSet { rebuildTVMenu() } }
|
2021-10-17 04:18:58 +05:30
|
|
|
|
|
2021-10-24 23:31:08 +05:30
|
|
|
|
@Published var queue = [PlayerQueueItem]() { didSet { Defaults[.queue] = queue } }
|
|
|
|
|
@Published var currentItem: PlayerQueueItem! { didSet { Defaults[.lastPlayed] = currentItem } }
|
|
|
|
|
@Published var history = [PlayerQueueItem]() { didSet { Defaults[.history] = history } }
|
2021-06-15 22:05:21 +05:30
|
|
|
|
|
2021-10-17 04:18:58 +05:30
|
|
|
|
@Published var savedTime: CMTime?
|
2021-06-18 04:13:29 +05:30
|
|
|
|
|
2021-10-23 04:34:03 +05:30
|
|
|
|
@Published var playerNavigationLinkActive = false
|
2021-08-23 00:43:33 +05:30
|
|
|
|
|
2021-10-23 22:19:45 +05:30
|
|
|
|
@Published var sponsorBlock = SponsorBlockAPI()
|
|
|
|
|
@Published var segmentRestorationTime: CMTime?
|
|
|
|
|
@Published var lastSkipped: Segment? { didSet { rebuildTVMenu() } }
|
|
|
|
|
@Published var restoredSegments = [Segment]()
|
|
|
|
|
|
2021-10-17 04:18:58 +05:30
|
|
|
|
var accounts: AccountsModel
|
2021-12-05 01:05:41 +05:30
|
|
|
|
var comments: CommentsModel
|
2021-10-06 01:50:09 +05:30
|
|
|
|
|
2021-10-23 04:34:03 +05:30
|
|
|
|
var composition = AVMutableComposition()
|
2021-11-28 20:07:55 +05:30
|
|
|
|
var loadedCompositionAssets = [AVMediaType]()
|
2021-10-24 23:31:08 +05:30
|
|
|
|
|
2021-10-29 01:48:23 +05:30
|
|
|
|
private var currentArtwork: MPMediaItemArtwork?
|
2021-10-24 23:31:08 +05:30
|
|
|
|
private var frequentTimeObserver: Any?
|
|
|
|
|
private var infrequentTimeObserver: Any?
|
|
|
|
|
private var playerTimeControlStatusObserver: Any?
|
|
|
|
|
|
2021-10-17 04:18:58 +05:30
|
|
|
|
private var statusObservation: NSKeyValueObservation?
|
|
|
|
|
|
2021-10-26 02:59:06 +05:30
|
|
|
|
private var timeObserverThrottle = Throttle(interval: 2)
|
|
|
|
|
|
2021-10-28 22:44:55 +05:30
|
|
|
|
var playingInPictureInPicture = false
|
|
|
|
|
|
2021-11-07 22:22:42 +05:30
|
|
|
|
@Published var presentingErrorDetails = false
|
|
|
|
|
var playerError: Error? { didSet {
|
|
|
|
|
#if !os(tvOS)
|
|
|
|
|
if !playerError.isNil {
|
|
|
|
|
presentingErrorDetails = true
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
}}
|
|
|
|
|
|
2021-12-05 01:05:41 +05:30
|
|
|
|
init(accounts: AccountsModel? = nil, comments: CommentsModel? = nil) {
|
2021-10-17 04:18:58 +05:30
|
|
|
|
self.accounts = accounts ?? AccountsModel()
|
2021-12-05 01:05:41 +05:30
|
|
|
|
self.comments = comments ?? CommentsModel()
|
2021-10-25 13:55:41 +05:30
|
|
|
|
|
2021-10-06 01:50:09 +05:30
|
|
|
|
addItemDidPlayToEndTimeObserver()
|
2021-10-24 23:31:08 +05:30
|
|
|
|
addFrequentTimeObserver()
|
|
|
|
|
addInfrequentTimeObserver()
|
|
|
|
|
addPlayerTimeControlStatusObserver()
|
|
|
|
|
}
|
2021-10-24 19:31:36 +05:30
|
|
|
|
|
2021-10-06 01:50:09 +05:30
|
|
|
|
func presentPlayer() {
|
|
|
|
|
presentingPlayer = true
|
2021-06-16 02:51:57 +05:30
|
|
|
|
}
|
|
|
|
|
|
2021-11-09 04:44:28 +05:30
|
|
|
|
func togglePlayer() {
|
|
|
|
|
presentingPlayer.toggle()
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-17 04:18:58 +05:30
|
|
|
|
var isPlaying: Bool {
|
|
|
|
|
player.timeControlStatus == .playing
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-23 02:19:31 +05:30
|
|
|
|
var time: CMTime? {
|
|
|
|
|
currentItem?.playbackTime
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-23 04:34:03 +05:30
|
|
|
|
var live: Bool {
|
2021-10-24 23:31:08 +05:30
|
|
|
|
currentItem?.video?.live ?? false
|
2021-10-23 04:34:03 +05:30
|
|
|
|
}
|
|
|
|
|
|
2021-10-23 02:19:31 +05:30
|
|
|
|
var playerItemDuration: CMTime? {
|
|
|
|
|
player.currentItem?.asset.duration
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var videoDuration: TimeInterval? {
|
|
|
|
|
currentItem?.duration ?? currentVideo?.length
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-06 01:50:09 +05:30
|
|
|
|
func togglePlay() {
|
|
|
|
|
isPlaying ? pause() : play()
|
2021-06-15 22:05:21 +05:30
|
|
|
|
}
|
|
|
|
|
|
2021-10-06 01:50:09 +05:30
|
|
|
|
func play() {
|
2021-10-24 23:31:08 +05:30
|
|
|
|
guard player.timeControlStatus != .playing else {
|
2021-06-15 22:05:21 +05:30
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-06 01:50:09 +05:30
|
|
|
|
player.play()
|
2021-07-30 03:58:28 +05:30
|
|
|
|
}
|
|
|
|
|
|
2021-10-06 01:50:09 +05:30
|
|
|
|
func pause() {
|
2021-10-24 23:31:08 +05:30
|
|
|
|
guard player.timeControlStatus != .paused else {
|
2021-07-30 03:58:28 +05:30
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-06 01:50:09 +05:30
|
|
|
|
player.pause()
|
2021-09-25 13:48:22 +05:30
|
|
|
|
}
|
|
|
|
|
|
2021-10-17 04:18:58 +05:30
|
|
|
|
func upgradeToStream(_ stream: Stream) {
|
|
|
|
|
if !self.stream.isNil, self.stream != stream {
|
2021-12-18 01:25:52 +05:30
|
|
|
|
playStream(stream, of: currentVideo!, preservingTime: true, upgrading: true)
|
2021-07-22 18:13:13 +05:30
|
|
|
|
}
|
2021-10-17 04:18:58 +05:30
|
|
|
|
}
|
2021-07-19 04:02:46 +05:30
|
|
|
|
|
2021-10-24 23:31:08 +05:30
|
|
|
|
func playStream(
|
2021-10-17 04:18:58 +05:30
|
|
|
|
_ stream: Stream,
|
|
|
|
|
of video: Video,
|
2021-12-18 01:25:52 +05:30
|
|
|
|
preservingTime: Bool = false,
|
|
|
|
|
upgrading: Bool = false
|
2021-10-17 04:18:58 +05:30
|
|
|
|
) {
|
2021-11-07 22:22:42 +05:30
|
|
|
|
playerError = nil
|
2021-12-18 01:25:52 +05:30
|
|
|
|
if !upgrading {
|
|
|
|
|
resetSegments()
|
|
|
|
|
|
|
|
|
|
sponsorBlock.loadSegments(
|
|
|
|
|
videoID: video.videoID,
|
|
|
|
|
categories: Defaults[.sponsorBlockCategories]
|
|
|
|
|
) { [weak self] in
|
|
|
|
|
if Defaults[.showChannelSubscribers] {
|
|
|
|
|
self?.loadCurrentItemChannelDetails()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-10-23 22:19:45 +05:30
|
|
|
|
|
2021-10-17 04:18:58 +05:30
|
|
|
|
if let url = stream.singleAssetURL {
|
|
|
|
|
logger.info("playing stream with one asset\(stream.kind == .hls ? " (HLS)" : ""): \(url)")
|
|
|
|
|
|
2021-10-23 02:19:31 +05:30
|
|
|
|
insertPlayerItem(stream, for: video, preservingTime: preservingTime)
|
2021-10-06 01:50:09 +05:30
|
|
|
|
} else {
|
2021-10-17 04:18:58 +05:30
|
|
|
|
logger.info("playing stream with many assets:")
|
|
|
|
|
logger.info("composition audio asset: \(stream.audioAsset.url)")
|
|
|
|
|
logger.info("composition video asset: \(stream.videoAsset.url)")
|
|
|
|
|
|
2021-11-28 20:07:55 +05:30
|
|
|
|
loadComposition(stream, of: video, preservingTime: preservingTime)
|
2021-07-19 04:02:46 +05:30
|
|
|
|
}
|
2021-10-29 01:48:23 +05:30
|
|
|
|
|
2021-12-18 01:25:52 +05:30
|
|
|
|
if !upgrading {
|
|
|
|
|
updateCurrentArtwork()
|
|
|
|
|
}
|
2021-07-19 04:02:46 +05:30
|
|
|
|
}
|
|
|
|
|
|
2021-11-05 20:28:51 +05:30
|
|
|
|
private func pauseOnPlayerDismiss() {
|
|
|
|
|
if !playingInPictureInPicture, !presentingPlayer {
|
|
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
|
|
|
|
|
self.pause()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-17 04:18:58 +05:30
|
|
|
|
private func insertPlayerItem(
|
|
|
|
|
_ stream: Stream,
|
|
|
|
|
for video: Video,
|
|
|
|
|
preservingTime: Bool = false
|
|
|
|
|
) {
|
|
|
|
|
let playerItem = playerItem(stream)
|
2021-10-06 01:50:09 +05:30
|
|
|
|
guard playerItem != nil else {
|
2021-07-19 04:02:46 +05:30
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-17 04:18:58 +05:30
|
|
|
|
attachMetadata(to: playerItem!, video: video, for: stream)
|
2021-10-23 02:19:31 +05:30
|
|
|
|
|
2021-10-24 23:31:08 +05:30
|
|
|
|
DispatchQueue.main.async { [weak self] in
|
|
|
|
|
guard let self = self else {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-06 01:50:09 +05:30
|
|
|
|
self.stream = stream
|
2021-10-17 04:18:58 +05:30
|
|
|
|
self.composition = AVMutableComposition()
|
2021-10-06 01:50:09 +05:30
|
|
|
|
}
|
|
|
|
|
|
2021-10-24 23:31:08 +05:30
|
|
|
|
let startPlaying = {
|
|
|
|
|
#if !os(macOS)
|
|
|
|
|
try? AVAudioSession.sharedInstance().setActive(true)
|
|
|
|
|
#endif
|
|
|
|
|
|
2021-11-02 03:26:18 +05:30
|
|
|
|
if self.isAutoplaying(playerItem!) {
|
|
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) { [weak self] in
|
|
|
|
|
self?.play()
|
|
|
|
|
}
|
2021-10-24 23:31:08 +05:30
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-23 02:19:31 +05:30
|
|
|
|
let replaceItemAndSeek = {
|
|
|
|
|
self.player.replaceCurrentItem(with: playerItem)
|
|
|
|
|
self.seekToSavedTime { finished in
|
|
|
|
|
guard finished else {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
self.savedTime = nil
|
2021-10-24 19:31:36 +05:30
|
|
|
|
|
2021-10-24 23:31:08 +05:30
|
|
|
|
startPlaying()
|
2021-10-23 02:19:31 +05:30
|
|
|
|
}
|
|
|
|
|
}
|
2021-07-19 04:02:46 +05:30
|
|
|
|
|
2021-10-17 04:18:58 +05:30
|
|
|
|
if preservingTime {
|
2021-10-23 02:19:31 +05:30
|
|
|
|
if savedTime.isNil {
|
|
|
|
|
saveTime {
|
|
|
|
|
replaceItemAndSeek()
|
|
|
|
|
startPlaying()
|
2021-10-17 04:18:58 +05:30
|
|
|
|
}
|
2021-10-23 02:19:31 +05:30
|
|
|
|
} else {
|
|
|
|
|
replaceItemAndSeek()
|
|
|
|
|
startPlaying()
|
2021-10-17 04:18:58 +05:30
|
|
|
|
}
|
2021-07-22 18:13:13 +05:30
|
|
|
|
} else {
|
2021-10-17 04:18:58 +05:30
|
|
|
|
player.replaceCurrentItem(with: playerItem)
|
2021-10-23 02:19:31 +05:30
|
|
|
|
startPlaying()
|
2021-06-15 22:05:21 +05:30
|
|
|
|
}
|
2021-06-17 00:42:41 +05:30
|
|
|
|
}
|
2021-06-15 22:05:21 +05:30
|
|
|
|
|
2021-10-17 04:18:58 +05:30
|
|
|
|
private func loadComposition(
|
|
|
|
|
_ stream: Stream,
|
|
|
|
|
of video: Video,
|
|
|
|
|
preservingTime: Bool = false
|
2021-11-28 20:07:55 +05:30
|
|
|
|
) {
|
|
|
|
|
loadedCompositionAssets = []
|
|
|
|
|
loadCompositionAsset(stream.audioAsset, stream: stream, type: .audio, of: video, preservingTime: preservingTime)
|
|
|
|
|
loadCompositionAsset(stream.videoAsset, stream: stream, type: .video, of: video, preservingTime: preservingTime)
|
2021-06-15 22:05:21 +05:30
|
|
|
|
}
|
2021-06-16 02:51:57 +05:30
|
|
|
|
|
2021-12-03 01:49:10 +05:30
|
|
|
|
private func loadCompositionAsset(
|
2021-10-24 23:31:08 +05:30
|
|
|
|
_ asset: AVURLAsset,
|
2021-11-28 20:07:55 +05:30
|
|
|
|
stream: Stream,
|
2021-10-24 23:31:08 +05:30
|
|
|
|
type: AVMediaType,
|
2021-11-28 20:07:55 +05:30
|
|
|
|
of video: Video,
|
|
|
|
|
preservingTime: Bool = false
|
|
|
|
|
) {
|
|
|
|
|
asset.loadValuesAsynchronously(forKeys: ["playable"]) { [weak self] in
|
|
|
|
|
guard let self = self else {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
self.logger.info("loading \(type.rawValue) track")
|
2021-06-17 00:42:41 +05:30
|
|
|
|
|
2021-11-28 20:07:55 +05:30
|
|
|
|
let assetTracks = asset.tracks(withMediaType: type)
|
|
|
|
|
|
|
|
|
|
guard let compositionTrack = self.composition.addMutableTrack(
|
|
|
|
|
withMediaType: type,
|
|
|
|
|
preferredTrackID: kCMPersistentTrackID_Invalid
|
|
|
|
|
) else {
|
|
|
|
|
self.logger.critical("composition \(type.rawValue) addMutableTrack FAILED")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
guard let assetTrack = assetTracks.first else {
|
|
|
|
|
self.logger.critical("asset \(type.rawValue) track FAILED")
|
|
|
|
|
return
|
|
|
|
|
}
|
2021-10-17 04:18:58 +05:30
|
|
|
|
|
2021-11-28 20:07:55 +05:30
|
|
|
|
try! compositionTrack.insertTimeRange(
|
|
|
|
|
CMTimeRange(start: .zero, duration: CMTime.secondsInDefaultTimescale(video.length)),
|
|
|
|
|
of: assetTrack,
|
|
|
|
|
at: .zero
|
|
|
|
|
)
|
2021-10-17 04:18:58 +05:30
|
|
|
|
|
2021-11-28 20:07:55 +05:30
|
|
|
|
self.logger.critical("\(type.rawValue) LOADED")
|
|
|
|
|
|
|
|
|
|
guard self.streamSelection == stream else {
|
|
|
|
|
self.logger.critical("IGNORING LOADED")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self.loadedCompositionAssets.append(type)
|
|
|
|
|
|
|
|
|
|
if self.loadedCompositionAssets.count == 2 {
|
|
|
|
|
self.insertPlayerItem(stream, for: video, preservingTime: preservingTime)
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-10-17 04:18:58 +05:30
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private func playerItem(_ stream: Stream) -> AVPlayerItem? {
|
|
|
|
|
if let url = stream.singleAssetURL {
|
|
|
|
|
return AVPlayerItem(asset: AVURLAsset(url: url))
|
|
|
|
|
} else {
|
|
|
|
|
return AVPlayerItem(asset: composition)
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-07-22 18:13:13 +05:30
|
|
|
|
|
2021-10-17 04:18:58 +05:30
|
|
|
|
private func attachMetadata(to item: AVPlayerItem, video: Video, for _: Stream? = nil) {
|
2021-07-22 18:13:13 +05:30
|
|
|
|
#if !os(macOS)
|
2021-10-17 04:18:58 +05:30
|
|
|
|
var externalMetadata = [
|
|
|
|
|
makeMetadataItem(.commonIdentifierTitle, value: video.title),
|
2021-10-21 03:51:50 +05:30
|
|
|
|
makeMetadataItem(.quickTimeMetadataGenre, value: video.genre ?? ""),
|
|
|
|
|
makeMetadataItem(.commonIdentifierDescription, value: video.description ?? "")
|
2021-10-17 04:18:58 +05:30
|
|
|
|
]
|
2021-10-06 01:50:09 +05:30
|
|
|
|
if let thumbnailData = try? Data(contentsOf: video.thumbnailURL(quality: .medium)!),
|
2021-07-22 18:13:13 +05:30
|
|
|
|
let image = UIImage(data: thumbnailData),
|
|
|
|
|
let pngData = image.pngData()
|
|
|
|
|
{
|
|
|
|
|
let artworkItem = makeMetadataItem(.commonIdentifierArtwork, value: pngData)
|
|
|
|
|
externalMetadata.append(artworkItem)
|
|
|
|
|
}
|
2021-06-16 02:51:57 +05:30
|
|
|
|
|
2021-10-17 04:18:58 +05:30
|
|
|
|
item.externalMetadata = externalMetadata
|
2021-07-22 18:13:13 +05:30
|
|
|
|
#endif
|
2021-06-16 02:51:57 +05:30
|
|
|
|
|
2021-10-17 04:18:58 +05:30
|
|
|
|
item.preferredForwardBufferDuration = 5
|
2021-06-16 02:51:57 +05:30
|
|
|
|
|
2021-10-06 01:50:09 +05:30
|
|
|
|
statusObservation?.invalidate()
|
2021-10-24 23:31:08 +05:30
|
|
|
|
statusObservation = item.observe(\.status, options: [.old, .new]) { [weak self] playerItem, _ in
|
|
|
|
|
guard let self = self else {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-06 01:50:09 +05:30
|
|
|
|
switch playerItem.status {
|
|
|
|
|
case .readyToPlay:
|
2021-10-24 23:31:08 +05:30
|
|
|
|
if self.isAutoplaying(playerItem) {
|
2021-10-17 04:18:58 +05:30
|
|
|
|
self.play()
|
2021-10-06 01:50:09 +05:30
|
|
|
|
}
|
2021-10-17 04:18:58 +05:30
|
|
|
|
case .failed:
|
2021-11-07 22:22:42 +05:30
|
|
|
|
self.playerError = item.error
|
2021-10-17 04:18:58 +05:30
|
|
|
|
|
2021-10-06 01:50:09 +05:30
|
|
|
|
default:
|
|
|
|
|
return
|
|
|
|
|
}
|
2021-06-18 15:47:01 +05:30
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-17 04:18:58 +05:30
|
|
|
|
#if !os(macOS)
|
|
|
|
|
private func makeMetadataItem(_ identifier: AVMetadataIdentifier, value: Any) -> AVMetadataItem {
|
|
|
|
|
let item = AVMutableMetadataItem()
|
|
|
|
|
|
|
|
|
|
item.identifier = identifier
|
|
|
|
|
item.value = value as? NSCopying & NSObjectProtocol
|
|
|
|
|
item.extendedLanguageTag = "und"
|
|
|
|
|
|
|
|
|
|
return item.copy() as! AVMetadataItem
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
private func addItemDidPlayToEndTimeObserver() {
|
2021-10-06 01:50:09 +05:30
|
|
|
|
NotificationCenter.default.addObserver(
|
|
|
|
|
self,
|
|
|
|
|
selector: #selector(itemDidPlayToEndTime),
|
|
|
|
|
name: NSNotification.Name.AVPlayerItemDidPlayToEndTime,
|
|
|
|
|
object: nil
|
|
|
|
|
)
|
2021-06-18 15:47:01 +05:30
|
|
|
|
}
|
|
|
|
|
|
2021-10-06 01:50:09 +05:30
|
|
|
|
@objc func itemDidPlayToEndTime() {
|
2021-10-24 23:31:08 +05:30
|
|
|
|
currentItem.playbackTime = playerItemDuration
|
|
|
|
|
|
2021-10-06 01:50:09 +05:30
|
|
|
|
if queue.isEmpty {
|
2021-10-25 03:56:25 +05:30
|
|
|
|
#if !os(macOS)
|
|
|
|
|
try? AVAudioSession.sharedInstance().setActive(false)
|
|
|
|
|
#endif
|
2021-10-23 02:19:31 +05:30
|
|
|
|
addCurrentItemToHistory()
|
2021-10-06 01:50:09 +05:30
|
|
|
|
resetQueue()
|
|
|
|
|
#if os(tvOS)
|
2021-10-24 23:31:08 +05:30
|
|
|
|
avPlayerViewController!.dismiss(animated: true) { [weak self] in
|
|
|
|
|
self?.controller!.dismiss(animated: true)
|
2021-10-06 01:50:09 +05:30
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
presentingPlayer = false
|
|
|
|
|
} else {
|
|
|
|
|
advanceToNextItem()
|
2021-06-16 02:51:57 +05:30
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-17 04:18:58 +05:30
|
|
|
|
private func saveTime(completionHandler: @escaping () -> Void = {}) {
|
|
|
|
|
let currentTime = player.currentTime()
|
|
|
|
|
|
|
|
|
|
guard currentTime.seconds > 0 else {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-24 23:31:08 +05:30
|
|
|
|
DispatchQueue.main.async { [weak self] in
|
|
|
|
|
self?.savedTime = currentTime
|
2021-10-17 04:18:58 +05:30
|
|
|
|
completionHandler()
|
2021-06-18 04:13:29 +05:30
|
|
|
|
}
|
2021-10-17 04:18:58 +05:30
|
|
|
|
}
|
2021-07-22 18:13:13 +05:30
|
|
|
|
|
2021-10-17 04:18:58 +05:30
|
|
|
|
private func seekToSavedTime(completionHandler: @escaping (Bool) -> Void = { _ in }) {
|
|
|
|
|
guard let time = savedTime else {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
player.seek(
|
|
|
|
|
to: time,
|
2021-10-24 23:31:08 +05:30
|
|
|
|
toleranceBefore: .secondsInDefaultTimescale(1),
|
2021-10-17 04:18:58 +05:30
|
|
|
|
toleranceAfter: .zero,
|
|
|
|
|
completionHandler: completionHandler
|
|
|
|
|
)
|
2021-06-18 04:13:29 +05:30
|
|
|
|
}
|
|
|
|
|
|
2021-10-24 23:31:08 +05:30
|
|
|
|
private func addFrequentTimeObserver() {
|
|
|
|
|
let interval = CMTime.secondsInDefaultTimescale(0.5)
|
|
|
|
|
|
|
|
|
|
frequentTimeObserver = player.addPeriodicTimeObserver(
|
|
|
|
|
forInterval: interval,
|
|
|
|
|
queue: .main
|
|
|
|
|
) { [weak self] _ in
|
|
|
|
|
guard let self = self else {
|
|
|
|
|
return
|
|
|
|
|
}
|
2021-06-20 01:40:14 +05:30
|
|
|
|
|
2021-10-23 22:19:45 +05:30
|
|
|
|
guard !self.currentItem.isNil else {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-03 01:10:49 +05:30
|
|
|
|
#if !os(tvOS)
|
|
|
|
|
self.updateNowPlayingInfo()
|
|
|
|
|
#endif
|
|
|
|
|
|
2021-10-24 23:31:08 +05:30
|
|
|
|
self.handleSegments(at: self.player.currentTime())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private func addInfrequentTimeObserver() {
|
|
|
|
|
let interval = CMTime.secondsInDefaultTimescale(5)
|
2021-10-23 22:19:45 +05:30
|
|
|
|
|
2021-10-24 23:31:08 +05:30
|
|
|
|
infrequentTimeObserver = player.addPeriodicTimeObserver(
|
|
|
|
|
forInterval: interval,
|
|
|
|
|
queue: .main
|
|
|
|
|
) { [weak self] _ in
|
|
|
|
|
guard let self = self else {
|
|
|
|
|
return
|
|
|
|
|
}
|
2021-10-23 22:19:45 +05:30
|
|
|
|
|
2021-10-24 23:31:08 +05:30
|
|
|
|
guard !self.currentItem.isNil else {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-26 02:59:06 +05:30
|
|
|
|
self.timeObserverThrottle.execute {
|
|
|
|
|
self.updateCurrentItemIntervals()
|
|
|
|
|
}
|
2021-06-18 04:13:29 +05:30
|
|
|
|
}
|
2021-06-17 00:42:41 +05:30
|
|
|
|
}
|
2021-10-24 19:31:36 +05:30
|
|
|
|
|
2021-10-24 23:31:08 +05:30
|
|
|
|
private func addPlayerTimeControlStatusObserver() {
|
|
|
|
|
playerTimeControlStatusObserver = player.observe(\.timeControlStatus) { [weak self] player, _ in
|
|
|
|
|
guard let self = self,
|
|
|
|
|
self.player == player
|
|
|
|
|
else {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-26 02:59:06 +05:30
|
|
|
|
if player.timeControlStatus != .waitingToPlayAtSpecifiedRate {
|
|
|
|
|
self.objectWillChange.send()
|
|
|
|
|
}
|
2021-10-24 23:31:08 +05:30
|
|
|
|
|
2021-11-02 22:54:59 +05:30
|
|
|
|
if player.timeControlStatus == .playing, player.rate != self.currentRate {
|
|
|
|
|
player.rate = self.currentRate
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-02 16:31:45 +05:30
|
|
|
|
#if os(macOS)
|
|
|
|
|
if player.timeControlStatus == .playing {
|
|
|
|
|
ScreenSaverManager.shared.disable(reason: "Yattee is playing video")
|
|
|
|
|
} else {
|
|
|
|
|
ScreenSaverManager.shared.enable()
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2021-10-26 02:59:06 +05:30
|
|
|
|
|
2021-11-02 16:31:45 +05:30
|
|
|
|
self.timeObserverThrottle.execute {
|
2021-10-26 02:59:06 +05:30
|
|
|
|
self.updateCurrentItemIntervals()
|
|
|
|
|
}
|
2021-10-24 19:31:36 +05:30
|
|
|
|
}
|
2021-10-24 23:31:08 +05:30
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private func updateCurrentItemIntervals() {
|
|
|
|
|
currentItem?.playbackTime = player.currentTime()
|
|
|
|
|
currentItem?.videoDuration = player.currentItem?.asset.duration.seconds
|
|
|
|
|
}
|
2021-10-29 01:48:23 +05:30
|
|
|
|
|
|
|
|
|
fileprivate func updateNowPlayingInfo() {
|
2021-11-06 01:05:27 +05:30
|
|
|
|
let duration: Int? = currentItem.video.live ? nil : Int(currentItem.videoDuration ?? 0)
|
2021-12-05 22:45:40 +05:30
|
|
|
|
var nowPlayingInfo: [String: AnyObject] = [
|
2021-10-29 01:48:23 +05:30
|
|
|
|
MPMediaItemPropertyTitle: currentItem.video.title as AnyObject,
|
|
|
|
|
MPMediaItemPropertyArtist: currentItem.video.author as AnyObject,
|
2021-11-06 01:05:27 +05:30
|
|
|
|
MPMediaItemPropertyPlaybackDuration: duration as AnyObject,
|
2021-11-02 03:26:18 +05:30
|
|
|
|
MPNowPlayingInfoPropertyIsLiveStream: currentItem.video.live as AnyObject,
|
2021-10-29 01:48:23 +05:30
|
|
|
|
MPNowPlayingInfoPropertyElapsedPlaybackTime: player.currentTime().seconds as AnyObject,
|
|
|
|
|
MPNowPlayingInfoPropertyPlaybackQueueCount: queue.count as AnyObject,
|
|
|
|
|
MPMediaItemPropertyMediaType: MPMediaType.anyVideo.rawValue as AnyObject
|
|
|
|
|
]
|
|
|
|
|
|
2021-12-05 22:45:40 +05:30
|
|
|
|
if !currentArtwork.isNil {
|
|
|
|
|
nowPlayingInfo[MPMediaItemPropertyArtwork] = currentArtwork as AnyObject
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-29 01:48:23 +05:30
|
|
|
|
MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private func updateCurrentArtwork() {
|
|
|
|
|
guard let thumbnailData = try? Data(contentsOf: currentItem.video.thumbnailURL(quality: .medium)!) else {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if os(macOS)
|
|
|
|
|
let image = NSImage(data: thumbnailData)
|
|
|
|
|
#else
|
|
|
|
|
let image = UIImage(data: thumbnailData)
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
if image.isNil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
currentArtwork = MPMediaItemArtwork(boundsSize: image!.size) { _ in image! }
|
|
|
|
|
}
|
2021-11-02 22:54:59 +05:30
|
|
|
|
|
|
|
|
|
func rateLabel(_ rate: Float) -> String {
|
|
|
|
|
let formatter = NumberFormatter()
|
|
|
|
|
formatter.minimumFractionDigits = 0
|
|
|
|
|
formatter.maximumFractionDigits = 2
|
|
|
|
|
|
|
|
|
|
return "\(formatter.string(from: NSNumber(value: rate))!)×"
|
|
|
|
|
}
|
2021-12-03 01:49:10 +05:30
|
|
|
|
|
|
|
|
|
func closeCurrentItem() {
|
|
|
|
|
addCurrentItemToHistory()
|
|
|
|
|
currentItem = nil
|
|
|
|
|
player.replaceCurrentItem(with: nil)
|
|
|
|
|
}
|
2021-06-14 23:35:02 +05:30
|
|
|
|
}
|