@@ -244,13 +244,28 @@ func mergeAssetWithSubtitles(videoAsset: AVURLAsset, subtitleAsset: AVURLAsset)
244
244
/// This value must be valid for the calculation to work correctly.
245
245
/// - Returns: A `CMTime` value representing the resolved seek position within the media.
246
246
@MainActor
247
- func getSeekTime( for time: Double , duration: CMTime ) -> CMTime ? {
248
- guard duration. isNumeric && duration. value != 0 else { return nil }
247
+ func getSeekTime( for time: Double , duration : CMTime ) -> CMTime ? {
249
248
250
- let endSeconds = CMTimeGetSeconds ( duration)
251
- let clampedSeconds = max ( 0 , min ( time, endSeconds) )
249
+ guard duration. value != 0 else { return nil }
250
+
251
+
252
+ let endTime = CMTimeGetSeconds ( duration)
253
+ let seekTime : CMTime
254
+
255
+ if time < 0 {
256
+ // If the time is negative, seek to the start of the video
257
+ seekTime = . zero
258
+ } else if time >= endTime {
259
+ // If the time exceeds the video duration, seek to the end of the video
260
+ let endCMTime = CMTime ( seconds: endTime, preferredTimescale: duration. timescale)
261
+ seekTime = endCMTime
262
+ } else {
263
+ // Otherwise, seek to the specified time
264
+ let seekCMTime = CMTime ( seconds: time, preferredTimescale: duration. timescale)
265
+ seekTime = seekCMTime
266
+ }
252
267
253
- return CMTime ( seconds : clampedSeconds , preferredTimescale : duration . timescale )
268
+ return seekTime
254
269
}
255
270
256
271
/// Creates an `AVPlayerItem` with optional subtitle merging.
0 commit comments