@@ -43,6 +43,8 @@ import AVKit
4343
4444 var providedData : [ String : Any ] ?
4545
46+
47+
4648 override init ( ) {
4749 super. init ( )
4850 setAnalyticsObservers ( )
@@ -128,22 +130,13 @@ import AVKit
128130 }
129131 }
130132
131- public func flushEvents( ) {
132- guard analytics else { return }
133- eventsManager. sendEvents ( )
134- }
135-
136- public func flushEventsAndCloseSession( ) {
137- guard analytics else { return }
138- eventsManager. sendViewEndEvent ( providedData: providedData)
139- flushEvents ( )
140- }
141-
142133 deinit {
143- guard analytics else { return }
144- removeObserver ( self , forKeyPath: PlayerKeyPath . status. rawValue)
145- removeObserver ( self , forKeyPath: PlayerKeyPath . timeControlStatus. rawValue)
146- flushEventsAndCloseSession ( )
134+ if analytics {
135+ removeObserver ( self , forKeyPath: PlayerKeyPath . status. rawValue)
136+ removeObserver ( self , forKeyPath: PlayerKeyPath . timeControlStatus. rawValue)
137+ eventsManager. sendViewEndEvent ( providedData: providedData)
138+ eventsManager. sendEvents ( )
139+ }
147140 }
148141
149142 func setAnalyticsObservers( ) {
@@ -178,6 +171,15 @@ import AVKit
178171
179172@available ( iOS 10 . 0 , * )
180173extension CLDVideoPlayer {
174+ func observeDuration( of playerItem: AVPlayerItem ) {
175+ let duration = playerItem. asset. duration
176+
177+ let durationInSeconds = Int ( CMTimeGetSeconds ( duration) )
178+ if !loadMetadataSent {
179+ loadMetadataSent = true
180+ self . eventsManager. sendLoadMetadataEvent ( duration: durationInSeconds)
181+ }
182+ }
181183 func handleStatusChanged( _ status: AVPlayer . Status ) {
182184 switch status {
183185 case . readyToPlay:
@@ -186,6 +188,7 @@ extension CLDVideoPlayer {
186188 eventsManager. sendViewStartEvent ( videoUrl: mediaURL. absoluteString, providedData: providedData)
187189 isIntialized = true
188190
191+ // Load duration asynchronously - more reliable on iOS 18+
189192 loadDurationAsynchronously ( )
190193 }
191194 break
@@ -256,7 +259,7 @@ extension CLDVideoPlayer {
256259 }
257260 }
258261
259- public func setProvidedData( data: [ String : Any ] ) {
262+ func setProvidedData( data: [ String : Any ] ) {
260263 providedData = data
261264 }
262265}
0 commit comments