Skip to content

Commit f999fe2

Browse files
authored
Fix CLDVideoPlayer duration for analytics
1 parent 11f859b commit f999fe2

2 files changed

Lines changed: 52 additions & 2 deletions

File tree

Cloudinary/Classes/ios/Video/CLDVideoPlayer.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,9 +217,17 @@ extension CLDVideoPlayer {
217217
switch status {
218218
case .loaded:
219219
let duration = asset.duration
220-
let durationInSeconds = Int(CMTimeGetSeconds(duration))
220+
var durationInSeconds = 0
221221

222-
if !self.loadMetadataSent && durationInSeconds > 0 && duration.isValid {
222+
if duration.isValid && duration.isNumeric && !duration.isIndefinite {
223+
let durationSeconds = CMTimeGetSeconds(duration)
224+
225+
if durationSeconds.isFinite && !durationSeconds.isNaN {
226+
durationInSeconds = Int(durationSeconds)
227+
}
228+
}
229+
230+
if !self.loadMetadataSent {
223231
self.loadMetadataSent = true
224232
self.eventsManager.sendLoadMetadataEvent(duration: durationInSeconds)
225233
}

Example/Tests/UIExtensions/CLDVideoPlayerTests.swift

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,46 @@ class CLDVideoPlayerTests: UIBaseTest {
7171

7272
wait(for: [expectation], timeout: 5.0)
7373
}
74+
75+
func testEventsManagerReceivesDurationValues() {
76+
let eventsManager = VideoEventsManager()
77+
78+
XCTAssertNoThrow({
79+
eventsManager.sendLoadMetadataEvent(duration: 0)
80+
}, "Sending duration 0 should not crash")
81+
82+
XCTAssertNoThrow({
83+
eventsManager.sendLoadMetadataEvent(duration: 10)
84+
}, "Sending valid duration should not crash")
85+
86+
XCTAssertNoThrow({
87+
eventsManager.sendLoadMetadataEvent(duration: -1)
88+
}, "Events manager should handle any integer value")
89+
90+
XCTAssertNoThrow({
91+
eventsManager.sendLoadMetadataEvent(duration: Int.max)
92+
}, "Events manager should handle large values")
93+
}
94+
95+
func testDurationHandlingFlowIntegration() {
96+
let expectation = XCTestExpectation(description: "Duration flow should complete")
97+
98+
let player = CLDVideoPlayer(url: "data:,")
99+
100+
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
101+
XCTAssertNoThrow({
102+
_ = player.currentItem
103+
_ = player.status
104+
}, "Accessing player properties should not crash")
105+
106+
XCTAssertNoThrow({
107+
player.play()
108+
player.pause()
109+
}, "Player methods should work after problematic URL")
110+
111+
expectation.fulfill()
112+
}
113+
114+
wait(for: [expectation], timeout: 2.0)
115+
}
74116
}

0 commit comments

Comments
 (0)