From e121db5b627f734dfcbf726b781f25ef537dcd66 Mon Sep 17 00:00:00 2001 From: Adam Sharp Date: Tue, 3 Sep 2024 15:07:27 -0400 Subject: [PATCH] Fix crash when showing video playback controls with an invalid time range --- .../MediaGallery/VideoPlaybackControls.swift | 20 +++++++------------ 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/Signal/src/ViewControllers/MediaGallery/VideoPlaybackControls.swift b/Signal/src/ViewControllers/MediaGallery/VideoPlaybackControls.swift index dd7f08291b..116deaae42 100644 --- a/Signal/src/ViewControllers/MediaGallery/VideoPlaybackControls.swift +++ b/Signal/src/ViewControllers/MediaGallery/VideoPlaybackControls.swift @@ -619,11 +619,11 @@ class PlayerProgressView: UIView { // MARK: Render cycle - private static var formatter: DateComponentsFormatter = { + private static let formatter: DateComponentsFormatter = { let formatter = DateComponentsFormatter() formatter.unitsStyle = .positional - formatter.allowedUnits = [.minute, .second ] - formatter.zeroFormattingBehavior = [ .pad ] + formatter.allowedUnits = [.minute, .second] + formatter.zeroFormattingBehavior = .pad return formatter }() @@ -639,14 +639,11 @@ class PlayerProgressView: UIView { } let position = avPlayer.currentTime() - let positionSeconds: Float64 = CMTimeGetSeconds(position) - positionLabel.text = Self.formatter.string(from: positionSeconds) + positionLabel.text = Self.formatter.string(from: position.seconds) + slider.setValue(Float(position.seconds), animated: false) - let duration: CMTime = item.asset.duration - let remainingTime = duration - position - let remainingSeconds = CMTimeGetSeconds(remainingTime) - - guard let remainingString = Self.formatter.string(from: remainingSeconds) else { + let timeRangeRemaining = CMTimeRange(start: avPlayer.currentTime(), duration: item.asset.duration) + guard timeRangeRemaining.isValid, let remainingString = Self.formatter.string(from: timeRangeRemaining.duration.seconds) else { owsFailDebug("unable to format time remaining") remainingLabel.text = "0:00" return @@ -654,14 +651,11 @@ class PlayerProgressView: UIView { // show remaining time as negative remainingLabel.text = "-\(remainingString)" - - slider.setValue(Float(positionSeconds), animated: false) } // Allows the user to tap anywhere on the slider to set it's position, // without first having to grab the thumb. private class TrackingSlider: UISlider { - override func beginTracking(_ touch: UITouch, with event: UIEvent?) -> Bool { return true }