|
| 1 | +// |
| 2 | +// SponsorSkipPlugin.swift |
| 3 | +// BilibiliLive |
| 4 | +// |
| 5 | +// Created by yicheng on 2/11/2024. |
| 6 | +// |
| 7 | + |
| 8 | +import AVKit |
| 9 | + |
| 10 | +class SponsorSkipPlugin: NSObject, CommonPlayerPlugin { |
| 11 | + private var clipInfos: [SponsorBlockRequest.SkipSegment] = [] |
| 12 | + private let bvid: String |
| 13 | + private let duration: Double |
| 14 | + private var observers = [Any]() |
| 15 | + private weak var playerVC: AVPlayerViewController? |
| 16 | + |
| 17 | + private var set = false |
| 18 | + |
| 19 | + init(bvid: String, duration: Int) { |
| 20 | + self.bvid = bvid |
| 21 | + self.duration = Double(duration) |
| 22 | + } |
| 23 | + |
| 24 | + func loadClips() async { |
| 25 | + do { |
| 26 | + clipInfos = try await SponsorBlockRequest.getSkipSegments(bvid: bvid) |
| 27 | + clipInfos = clipInfos.filter { |
| 28 | + abs(duration - $0.videoDuration) < 4 |
| 29 | + } |
| 30 | + |
| 31 | + Logger.debug("[SponsorBlockRequest] get segs:" + clipInfos.map { "\($0.start)-\($0.end)" }.joined(separator: ",")) |
| 32 | + if !set, let player = await playerVC?.player { |
| 33 | + set = true |
| 34 | + sendClipToPlayer(player: player) |
| 35 | + } |
| 36 | + } catch { |
| 37 | + print(error) |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + func sendClipToPlayer(player: AVPlayer) { |
| 42 | + for clip in clipInfos { |
| 43 | + let start: CMTime |
| 44 | + let end: CMTime |
| 45 | + |
| 46 | + let buttonText: String |
| 47 | + let autoSkip = Settings.enableSponsorBlock == .jump |
| 48 | + if autoSkip { |
| 49 | + start = CMTime(seconds: clip.start - 5, preferredTimescale: 1) |
| 50 | + end = CMTime(seconds: clip.start, preferredTimescale: 1) |
| 51 | + buttonText = "取消跳过广告" |
| 52 | + } else { |
| 53 | + start = CMTime(seconds: clip.start, preferredTimescale: 1) |
| 54 | + end = CMTime(seconds: clip.end - 1, preferredTimescale: 1) |
| 55 | + buttonText = "跳过广告" |
| 56 | + } |
| 57 | + |
| 58 | + let skipAction = { [weak player, weak self] in |
| 59 | + player?.seek(to: CMTime(seconds: Double(clip.end), preferredTimescale: 1), toleranceBefore: .zero, toleranceAfter: .zero) |
| 60 | + self?.playerVC?.contextualActions = [] |
| 61 | + } |
| 62 | + |
| 63 | + let startObserver = player.addBoundaryTimeObserver(forTimes: [NSValue(time: start)], queue: .main) { |
| 64 | + [weak self] in |
| 65 | + guard let self = self else { return } |
| 66 | + let action: UIAction |
| 67 | + let identifier = UIAction.Identifier(clip.UUID) |
| 68 | + if autoSkip { |
| 69 | + action = UIAction(title: buttonText, identifier: identifier) { [weak self] _ in |
| 70 | + self?.playerVC?.contextualActions = [] |
| 71 | + } |
| 72 | + } else { |
| 73 | + action = UIAction(title: buttonText, identifier: identifier) { _ in skipAction() } |
| 74 | + } |
| 75 | + playerVC?.contextualActions = [action] |
| 76 | + } |
| 77 | + observers.append(startObserver) |
| 78 | + |
| 79 | + let endObserver = player.addBoundaryTimeObserver(forTimes: [NSValue(time: end)], queue: .main) { |
| 80 | + [weak self] in |
| 81 | + guard let self = self else { return } |
| 82 | + if let action = playerVC?.contextualActions.first, |
| 83 | + action.identifier.rawValue == clip.UUID, autoSkip |
| 84 | + { |
| 85 | + skipAction() |
| 86 | + } |
| 87 | + playerVC?.contextualActions = [] |
| 88 | + } |
| 89 | + observers.append(endObserver) |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + func playerDidLoad(playerVC: AVPlayerViewController) { |
| 94 | + self.playerVC = playerVC |
| 95 | + Task { |
| 96 | + await loadClips() |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + func playerWillStart(player: AVPlayer) { |
| 101 | + if !clipInfos.isEmpty { |
| 102 | + set = true |
| 103 | + sendClipToPlayer(player: player) |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + func playerDidCleanUp(player: AVPlayer) { |
| 108 | + for observer in observers { |
| 109 | + player.removeTimeObserver(observer) |
| 110 | + } |
| 111 | + } |
| 112 | +} |
0 commit comments