From 6b593c90e655f1004a32bcb0e0b305c3a39f8608 Mon Sep 17 00:00:00 2001 From: Igor Date: Thu, 15 Aug 2024 15:01:45 +0200 Subject: [PATCH] refactoring --- .../protocol/player/AbstractPlayer.swift | 20 ++++++++++++++----- .../player/LoopingPlayerProtocol.swift | 6 +++++- .../loop/player/ios/LoopingPlayerUIView.swift | 4 ++++ .../loop/player/mac/LoopingPlayerNSView.swift | 4 ++++ 4 files changed, 28 insertions(+), 6 deletions(-) diff --git a/Sources/swiftui-loop-videoplayer/protocol/player/AbstractPlayer.swift b/Sources/swiftui-loop-videoplayer/protocol/player/AbstractPlayer.swift index 1363220..f054ffc 100644 --- a/Sources/swiftui-loop-videoplayer/protocol/player/AbstractPlayer.swift +++ b/Sources/swiftui-loop-videoplayer/protocol/player/AbstractPlayer.swift @@ -35,6 +35,9 @@ public protocol AbstractPlayer: AnyObject { /// The queue player that plays the video items. var player: AVQueuePlayer? { get set } + /// A Boolean value indicating whether the player is currently seeking to a new time. + var isSeeking : Bool { get set } + // Playback control methods /// Initiates or resumes playback of the video. @@ -142,19 +145,26 @@ extension AbstractPlayer{ return } + isSeeking = true + let endTime = CMTimeGetSeconds(duration) + let seekTime : CMTime - if time < 0 { + if time <= 0 { // If the time is negative, seek to the start of the video - player.seek(to: .zero) - } else if time > endTime { + seekTime = .zero + } else if time >= endTime { // If the time exceeds the video duration, seek to the end of the video let endCMTime = CMTime(seconds: endTime, preferredTimescale: duration.timescale) - player.seek(to: endCMTime) + seekTime = endCMTime } else { // Otherwise, seek to the specified time let seekCMTime = CMTime(seconds: time, preferredTimescale: duration.timescale) - player.seek(to: seekCMTime) + seekTime = seekCMTime + } + + player.seek(to: seekTime){ [weak self] value in + self?.isSeeking = false } } diff --git a/Sources/swiftui-loop-videoplayer/protocol/player/LoopingPlayerProtocol.swift b/Sources/swiftui-loop-videoplayer/protocol/player/LoopingPlayerProtocol.swift index 7e310fe..f89dc3e 100644 --- a/Sources/swiftui-loop-videoplayer/protocol/player/LoopingPlayerProtocol.swift +++ b/Sources/swiftui-loop-videoplayer/protocol/player/LoopingPlayerProtocol.swift @@ -145,7 +145,11 @@ internal extension LoopingPlayerProtocol { if let timePublishing{ timeObserverToken = player.addPeriodicTimeObserver(forInterval: timePublishing, queue: .main) { [weak self] time in - self?.delegate?.didPassedTime(seconds: time.seconds) + guard let self = self else{ return } + + if !self.isSeeking{ + self.delegate?.didPassedTime(seconds: time.seconds) + } } } diff --git a/Sources/swiftui-loop-videoplayer/view/loop/player/ios/LoopingPlayerUIView.swift b/Sources/swiftui-loop-videoplayer/view/loop/player/ios/LoopingPlayerUIView.swift index ba3a446..82cec45 100644 --- a/Sources/swiftui-loop-videoplayer/view/loop/player/ios/LoopingPlayerUIView.swift +++ b/Sources/swiftui-loop-videoplayer/view/loop/player/ios/LoopingPlayerUIView.swift @@ -27,6 +27,7 @@ class LoopingPlayerUIView: UIView, LoopingPlayerProtocol { /// `contrast` indicates the level of contrast adjustment for the video content. internal var contrast: Float = 1 + /// A CALayer instance used for composing content, accessible only within the module. internal let compositeLayer = CALayer() /// The AVPlayerLayer that displays the video content. @@ -41,6 +42,9 @@ class LoopingPlayerUIView: UIView, LoopingPlayerProtocol { /// Declare a variable to hold the time observer token outside the if statement internal var timeObserverToken: Any? + /// A Boolean value indicating whether the player is currently seeking to a new time. + internal var isSeeking: Bool = false + /// Observer for errors from the AVQueuePlayer. internal var errorObserver: NSKeyValueObservation? diff --git a/Sources/swiftui-loop-videoplayer/view/loop/player/mac/LoopingPlayerNSView.swift b/Sources/swiftui-loop-videoplayer/view/loop/player/mac/LoopingPlayerNSView.swift index 616e1a9..c93d6ec 100644 --- a/Sources/swiftui-loop-videoplayer/view/loop/player/mac/LoopingPlayerNSView.swift +++ b/Sources/swiftui-loop-videoplayer/view/loop/player/mac/LoopingPlayerNSView.swift @@ -29,6 +29,7 @@ class LoopingPlayerNSView: NSView, LoopingPlayerProtocol { /// `contrast` indicates the level of contrast adjustment for the video content. internal var contrast: Float = 1 + /// A CALayer instance used for composing content, accessible only within the module. internal let compositeLayer = CALayer() /// The AVPlayerLayer that displays the video content. @@ -43,6 +44,9 @@ class LoopingPlayerNSView: NSView, LoopingPlayerProtocol { /// Declare a variable to hold the time observer token outside the if statement internal var timeObserverToken: Any? + /// A Boolean value indicating whether the player is currently seeking to a new time. + internal var isSeeking: Bool = false + /// Observer for errors from the AVQueuePlayer. internal var errorObserver: NSKeyValueObservation?