Skip to content

Commit 89a8f58

Browse files
committed
update
1 parent 549152e commit 89a8f58

File tree

5 files changed

+19
-26
lines changed

5 files changed

+19
-26
lines changed

β€ŽSources/swiftui-loop-videoplayer/fn/fn+.swift

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -243,27 +243,14 @@ func mergeAssetWithSubtitles(videoAsset: AVURLAsset, subtitleAsset: AVURLAsset)
243243
/// - duration: A `CMTime` value representing the total duration of the media.
244244
/// This value must be valid for the calculation to work correctly.
245245
/// - Returns: A `CMTime` value representing the resolved seek position within the media.
246-
func getSeekTime(for time: Double, duration : CMTime) -> CMTime?{
246+
@MainActor
247+
func getSeekTime(for time: Double, duration: CMTime) -> CMTime? {
248+
guard duration.isNumeric && duration.value != 0 else { return nil }
247249

248-
guard duration.value != 0 else{ return nil }
250+
let endSeconds = CMTimeGetSeconds(duration)
251+
let clampedSeconds = max(0, min(time, endSeconds))
249252

250-
let endTime = CMTimeGetSeconds(duration)
251-
let seekTime : CMTime
252-
253-
if time < 0 {
254-
// If the time is negative, seek to the start of the video
255-
seekTime = .zero
256-
} else if time >= endTime {
257-
// If the time exceeds the video duration, seek to the end of the video
258-
let endCMTime = CMTime(seconds: endTime, preferredTimescale: duration.timescale)
259-
seekTime = endCMTime
260-
} else {
261-
// Otherwise, seek to the specified time
262-
let seekCMTime = CMTime(seconds: time, preferredTimescale: duration.timescale)
263-
seekTime = seekCMTime
264-
}
265-
266-
return seekTime
253+
return CMTime(seconds: clampedSeconds, preferredTimescale: duration.timescale)
267254
}
268255

269256
/// Creates an `AVPlayerItem` with optional subtitle merging.

β€ŽSources/swiftui-loop-videoplayer/protocol/player/AbstractPlayer.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,8 @@ public protocol AbstractPlayer: AnyObject {
137137
func update(settings: VideoSettings)
138138
}
139139

140-
extension AbstractPlayer{
140+
@MainActor
141+
public extension AbstractPlayer{
141142

142143
/// Retrieves the current item being played.
143144
///

β€ŽSources/swiftui-loop-videoplayer/protocol/player/ExtPlayerProtocol.swift

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ public protocol ExtPlayerProtocol: AbstractPlayer, LayerMakerProtocol{
7171
func onError(_ error : VPErrors)
7272
}
7373

74+
@MainActor
7475
internal extension ExtPlayerProtocol {
7576

7677
/// Initializes a new player view with a video asset and custom settings.
@@ -133,11 +134,13 @@ internal extension ExtPlayerProtocol {
133134
/// - player: The `AVQueuePlayer` instance to which the time observer will be added.
134135
/// - settings: A `VideoSettings` object containing the time publishing interval and related configuration.
135136
func configureTimePublishing(_ player: AVQueuePlayer, _ settings: VideoSettings) {
136-
if let timePublishing = settings.timePublishing{
137-
timeObserver = player.addPeriodicTimeObserver(forInterval: timePublishing, queue: .global()) { [weak self] time in
138-
guard let self = self else{ return }
137+
if let timePublishing = settings.timePublishing {
138+
timeObserver = player.addPeriodicTimeObserver(
139+
forInterval: timePublishing,
140+
queue: .main
141+
) { [weak self] time in
139142
Task { @MainActor in
140-
self.delegate?.didPassedTime(seconds: time.seconds)
143+
self?.delegate?.didPassedTime(seconds: time.seconds)
141144
}
142145
}
143146
}

β€ŽSources/swiftui-loop-videoplayer/view/player/ios/ExtPlayerUIView.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,9 @@ internal class ExtPlayerUIView: UIView, ExtPlayerProtocol{
141141
func setupPiP(delegate: AVPictureInPictureControllerDelegate) {
142142
// Check if PiP is supported
143143
guard AVPictureInPictureController.isPictureInPictureSupported() else {
144-
DispatchQueue.main.asyncAfter(deadline: .now() + 1){ [weak self] in
145-
self?.onError(.notSupportedPiP)
144+
Task { @MainActor in
145+
try? await Task.sleep(nanoseconds: 1_000_000_000)
146+
self.onError(.notSupportedPiP)
146147
}
147148
return
148149
}

β€ŽSources/swiftui-loop-videoplayer/view/player/mac/ExtPlayerNSView.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ internal class ExtPlayerNSView: NSView, ExtPlayerProtocol {
7777
player = AVQueuePlayer(items: [])
7878

7979
super.init(frame: .zero)
80+
self.wantsLayer = true
8081

8182
addPlayerLayer()
8283
addCompositeLayer(settings)

0 commit comments

Comments
 (0)