Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
swiftuiux committed Aug 15, 2024
1 parent 83213f8 commit 6b593c9
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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?

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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?

Expand Down

0 comments on commit 6b593c9

Please sign in to comment.