Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4ef6426

Browse files
update
1 parent 509c13a commit 4ef6426

File tree

2 files changed

+41
-19
lines changed

2 files changed

+41
-19
lines changed

‎Sources/swiftui-loop-videoplayer/fn/fn+.swift‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,3 +207,33 @@ func mergeAssetWithSubtitles(videoAsset: AVURLAsset, subtitleAsset: AVURLAsset)
207207
return videoAsset
208208
#endif
209209
}
210+
211+
/// Determines the seek time as a `CMTime` based on a specified time and the total duration of the media.
212+
/// The function ensures that the seek time is within valid bounds (start to end of the media).
213+
///
214+
/// - Parameters:
215+
/// - time: A `Double` value representing the desired time to seek to, in seconds.
216+
/// If the value is negative, the function will seek to the start of the media.
217+
/// If the value exceeds the total duration, the function will seek to the end.
218+
/// - duration: A `CMTime` value representing the total duration of the media.
219+
/// This value must be valid for the calculation to work correctly.
220+
/// - Returns: A `CMTime` value representing the resolved seek position within the media.
221+
func getSeekTime(for time: Double, duration : CMTime) -> CMTime{
222+
let endTime = CMTimeGetSeconds(duration)
223+
let seekTime : CMTime
224+
225+
if time < 0 {
226+
// If the time is negative, seek to the start of the video
227+
seekTime = .zero
228+
} else if time >= endTime {
229+
// If the time exceeds the video duration, seek to the end of the video
230+
let endCMTime = CMTime(seconds: endTime, preferredTimescale: duration.timescale)
231+
seekTime = endCMTime
232+
} else {
233+
// Otherwise, seek to the specified time
234+
let seekCMTime = CMTime(seconds: time, preferredTimescale: duration.timescale)
235+
seekTime = seekCMTime
236+
}
237+
238+
return seekTime
239+
}

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

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -243,10 +243,16 @@ extension AbstractPlayer{
243243
}
244244
}
245245

246-
/// Seeks the video to a specific time.
247-
/// This method moves the playback position to the specified time with precise accuracy.
248-
/// If the specified time is out of bounds, it will be clamped to the nearest valid time.
249-
/// - Parameter time: The target time to seek to in the video timeline.
246+
/// Seeks the video to a specific time in the timeline.
247+
/// This method adjusts the playback position to the specified time with precise accuracy.
248+
/// If the target time is out of bounds (negative or beyond the duration), it will be clamped to the nearest valid time (start or end of the video).
249+
///
250+
/// - Parameters:
251+
/// - time: A `Double` value representing the target time (in seconds) to seek to in the video timeline.
252+
/// If the value is less than 0, the playback position will be set to the start of the video.
253+
/// If the value exceeds the video's duration, it will be set to the end.
254+
/// - play: A `Bool` value indicating whether to start playback immediately after seeking.
255+
/// Defaults to `false`, meaning playback will remain paused after the seek operation.
250256
func seek(to time: Double, play: Bool = false) {
251257
guard let player = player, let duration = player.currentItem?.duration else {
252258
guard let settings = currentSettings else{
@@ -274,21 +280,7 @@ extension AbstractPlayer{
274280
return
275281
}
276282

277-
let endTime = CMTimeGetSeconds(duration)
278-
let seekTime : CMTime
279-
280-
if time < 0 {
281-
// If the time is negative, seek to the start of the video
282-
seekTime = .zero
283-
} else if time >= endTime {
284-
// If the time exceeds the video duration, seek to the end of the video
285-
let endCMTime = CMTime(seconds: endTime, preferredTimescale: duration.timescale)
286-
seekTime = endCMTime
287-
} else {
288-
// Otherwise, seek to the specified time
289-
let seekCMTime = CMTime(seconds: time, preferredTimescale: duration.timescale)
290-
seekTime = seekCMTime
291-
}
283+
let seekTime = getSeekTime(for: time, duration: duration)
292284

293285
player.seek(to: seekTime){ [weak self] value in
294286
let currentTime = CMTimeGetSeconds(player.currentTime())

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /