Skip to content

Commit 6b593c9

Browse files
committed
refactoring
1 parent 83213f8 commit 6b593c9

File tree

4 files changed

+28
-6
lines changed

4 files changed

+28
-6
lines changed

Sources/swiftui-loop-videoplayer/protocol/player/AbstractPlayer.swift

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ public protocol AbstractPlayer: AnyObject {
3535
/// The queue player that plays the video items.
3636
var player: AVQueuePlayer? { get set }
3737

38+
/// A Boolean value indicating whether the player is currently seeking to a new time.
39+
var isSeeking : Bool { get set }
40+
3841
// Playback control methods
3942

4043
/// Initiates or resumes playback of the video.
@@ -142,19 +145,26 @@ extension AbstractPlayer{
142145
return
143146
}
144147

148+
isSeeking = true
149+
145150
let endTime = CMTimeGetSeconds(duration)
151+
let seekTime : CMTime
146152

147-
if time < 0 {
153+
if time <= 0 {
148154
// If the time is negative, seek to the start of the video
149-
player.seek(to: .zero)
150-
} else if time > endTime {
155+
seekTime = .zero
156+
} else if time >= endTime {
151157
// If the time exceeds the video duration, seek to the end of the video
152158
let endCMTime = CMTime(seconds: endTime, preferredTimescale: duration.timescale)
153-
player.seek(to: endCMTime)
159+
seekTime = endCMTime
154160
} else {
155161
// Otherwise, seek to the specified time
156162
let seekCMTime = CMTime(seconds: time, preferredTimescale: duration.timescale)
157-
player.seek(to: seekCMTime)
163+
seekTime = seekCMTime
164+
}
165+
166+
player.seek(to: seekTime){ [weak self] value in
167+
self?.isSeeking = false
158168
}
159169
}
160170

Sources/swiftui-loop-videoplayer/protocol/player/LoopingPlayerProtocol.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,11 @@ internal extension LoopingPlayerProtocol {
145145

146146
if let timePublishing{
147147
timeObserverToken = player.addPeriodicTimeObserver(forInterval: timePublishing, queue: .main) { [weak self] time in
148-
self?.delegate?.didPassedTime(seconds: time.seconds)
148+
guard let self = self else{ return }
149+
150+
if !self.isSeeking{
151+
self.delegate?.didPassedTime(seconds: time.seconds)
152+
}
149153
}
150154
}
151155

Sources/swiftui-loop-videoplayer/view/loop/player/ios/LoopingPlayerUIView.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class LoopingPlayerUIView: UIView, LoopingPlayerProtocol {
2727
/// `contrast` indicates the level of contrast adjustment for the video content.
2828
internal var contrast: Float = 1
2929

30+
/// A CALayer instance used for composing content, accessible only within the module.
3031
internal let compositeLayer = CALayer()
3132

3233
/// The AVPlayerLayer that displays the video content.
@@ -41,6 +42,9 @@ class LoopingPlayerUIView: UIView, LoopingPlayerProtocol {
4142
/// Declare a variable to hold the time observer token outside the if statement
4243
internal var timeObserverToken: Any?
4344

45+
/// A Boolean value indicating whether the player is currently seeking to a new time.
46+
internal var isSeeking: Bool = false
47+
4448
/// Observer for errors from the AVQueuePlayer.
4549
internal var errorObserver: NSKeyValueObservation?
4650

Sources/swiftui-loop-videoplayer/view/loop/player/mac/LoopingPlayerNSView.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class LoopingPlayerNSView: NSView, LoopingPlayerProtocol {
2929
/// `contrast` indicates the level of contrast adjustment for the video content.
3030
internal var contrast: Float = 1
3131

32+
/// A CALayer instance used for composing content, accessible only within the module.
3233
internal let compositeLayer = CALayer()
3334

3435
/// The AVPlayerLayer that displays the video content.
@@ -43,6 +44,9 @@ class LoopingPlayerNSView: NSView, LoopingPlayerProtocol {
4344
/// Declare a variable to hold the time observer token outside the if statement
4445
internal var timeObserverToken: Any?
4546

47+
/// A Boolean value indicating whether the player is currently seeking to a new time.
48+
internal var isSeeking: Bool = false
49+
4650
/// Observer for errors from the AVQueuePlayer.
4751
internal var errorObserver: NSKeyValueObservation?
4852

0 commit comments

Comments
 (0)