From 4509b6229a5628039c6184f5fdf8f1d01711c783 Mon Sep 17 00:00:00 2001 From: Arkadiusz Fal Date: Tue, 14 Jun 2022 23:20:19 +0200 Subject: [PATCH] Fix #163 --- Model/Player/PlayerSponsorBlock.swift | 36 +++++++++++++++++++++------ Model/Segment.swift | 2 +- 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/Model/Player/PlayerSponsorBlock.swift b/Model/Player/PlayerSponsorBlock.swift index 4b342067..d6628aca 100644 --- a/Model/Player/PlayerSponsorBlock.swift +++ b/Model/Player/PlayerSponsorBlock.swift @@ -1,4 +1,5 @@ import CoreMedia +import AVFAudio import Defaults import Foundation @@ -18,23 +19,42 @@ extension PlayerModel { var nextSegments = [firstSegment] while let segment = sponsorBlock.segments.first(where: { - $0.timeInSegment(.secondsInDefaultTimescale(nextSegments.last!.end + 2)) + !nextSegments.contains($0) && + $0.timeInSegment(.secondsInDefaultTimescale(nextSegments.last!.end + 2)) }) { nextSegments.append(segment) } - if let segmentToSkip = nextSegments.last(where: { $0.endTime <= playerItemDuration ?? .zero }), - shouldSkip(segmentToSkip, at: time) - { + if let segmentToSkip = nextSegments.last, shouldSkip(segmentToSkip, at: time) { skip(segmentToSkip, at: time) } } private func skip(_ segment: Segment, at time: CMTime) { - guard segment.endTime.seconds <= playerItemDuration?.seconds ?? .infinity else { - logger.error( - "segment end time is: \(segment.end) when player item duration is: \(playerItemDuration?.seconds ?? .infinity)" - ) + if let duration = playerItemDuration, segment.endTime.seconds >= duration.seconds - 3 { + logger.error("segment end time is: \(segment.end) when player item duration is: \(duration.seconds)") + + + DispatchQueue.main.async { [weak self] in + guard let self = self else { + return + } + + self.prepareCurrentItemForHistory(finished: true) + self.pause() + + if self.queue.isEmpty { + #if !os(macOS) + try? AVAudioSession.sharedInstance().setActive(false) + #endif + + self.resetQueue() + self.hide() + } else { + self.advanceToNextItem() + } + } + return } diff --git a/Model/Segment.swift b/Model/Segment.swift index fb53af02..951926a2 100644 --- a/Model/Segment.swift +++ b/Model/Segment.swift @@ -22,7 +22,7 @@ class Segment: ObservableObject, Hashable { } var endTime: CMTime { - CMTime(seconds: end, preferredTimescale: 1000) + .secondsInDefaultTimescale(end) } init(category: String, segment: [Double], uuid: String, videoDuration: Int) {