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 a5f177e

Browse files
Update fn+.swift
1 parent bfdbb57 commit a5f177e

File tree

1 file changed

+16
-21
lines changed
  • Sources/swiftui-loop-videoplayer/fn

1 file changed

+16
-21
lines changed

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

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -243,27 +243,22 @@ 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?{
247-
248-
guard duration.value != 0 else{ return nil }
249-
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
246+
func getSeekTime(for time: Double, duration: CMTime) -> CMTime? {
247+
guard duration.isValid,
248+
duration.isNumeric,
249+
duration.timescale != 0
250+
else { return nil }
251+
252+
let endSeconds = CMTimeGetSeconds(duration)
253+
guard endSeconds.isFinite, endSeconds >= 0 else { return nil }
254+
255+
if time <= 0 { return .zero }
256+
if time >= endSeconds { return duration }
257+
258+
let clamped = max(0, min(time, endSeconds))
259+
260+
let scale = max(Int32(600), duration.timescale)
261+
return CMTime(seconds: clamped, preferredTimescale: scale)
267262
}
268263

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

0 commit comments

Comments
(0)

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