2022-06-18 12:39:49 +00:00
|
|
|
import CoreMedia
|
|
|
|
import Foundation
|
2022-08-28 17:18:49 +00:00
|
|
|
import SwiftUI
|
2022-06-18 12:39:49 +00:00
|
|
|
|
|
|
|
final class PlayerTimeModel: ObservableObject {
|
2022-08-31 19:24:46 +00:00
|
|
|
static let shared = PlayerTimeModel()
|
2022-06-18 12:39:49 +00:00
|
|
|
static let timePlaceholder = "--:--"
|
|
|
|
|
|
|
|
@Published var currentTime = CMTime.zero
|
|
|
|
@Published var duration = CMTime.zero
|
|
|
|
|
2022-08-28 17:18:49 +00:00
|
|
|
var player: PlayerModel!
|
2022-06-18 12:39:49 +00:00
|
|
|
|
2022-08-27 00:43:47 +00:00
|
|
|
var forceHours: Bool {
|
|
|
|
duration.seconds >= 60 * 60
|
|
|
|
}
|
|
|
|
|
2022-06-18 12:39:49 +00:00
|
|
|
var currentPlaybackTime: String {
|
|
|
|
if player?.currentItem.isNil ?? true || duration.seconds.isZero {
|
|
|
|
return Self.timePlaceholder
|
|
|
|
}
|
|
|
|
|
2022-08-27 00:43:47 +00:00
|
|
|
return currentTime.seconds.formattedAsPlaybackTime(allowZero: true, forceHours: forceHours) ?? Self.timePlaceholder
|
2022-06-18 12:39:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var durationPlaybackTime: String {
|
|
|
|
if player?.currentItem.isNil ?? true {
|
|
|
|
return Self.timePlaceholder
|
|
|
|
}
|
|
|
|
|
|
|
|
return duration.seconds.formattedAsPlaybackTime() ?? Self.timePlaceholder
|
|
|
|
}
|
|
|
|
|
|
|
|
var withoutSegmentsPlaybackTime: String {
|
2022-08-28 17:18:49 +00:00
|
|
|
guard let withoutSegmentsDuration = player?.playerItemDurationWithoutSponsorSegments?.seconds else { return Self.timePlaceholder }
|
|
|
|
return withoutSegmentsDuration.formattedAsPlaybackTime(forceHours: forceHours) ?? Self.timePlaceholder
|
|
|
|
}
|
2022-06-18 12:39:49 +00:00
|
|
|
}
|