@@ -244,28 +244,13 @@ 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 ? {
247
+ func getSeekTime( for time: Double , duration: CMTime ) -> CMTime ? {
248
+ guard duration. isNumeric && duration. value != 0 else { return nil }
248
249
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
- }
250
+ let endSeconds = CMTimeGetSeconds ( duration)
251
+ let clampedSeconds = max ( 0 , min ( time, endSeconds) )
267
252
268
- return seekTime
253
+ return CMTime ( seconds : clampedSeconds , preferredTimescale : duration . timescale )
269
254
}
270
255
271
256
/// Creates an `AVPlayerItem` with optional subtitle merging.
0 commit comments