From bada1aee8767dadab1450194fc7acd94e268047c Mon Sep 17 00:00:00 2001 From: Romain Petit Date: Tue, 22 Nov 2022 11:54:55 +0100 Subject: [PATCH 1/2] fix() use completionhandler for seek, and remove redundant analytics --- .../ApiVideoPlayerController.swift | 58 ++++++++++--------- 1 file changed, 30 insertions(+), 28 deletions(-) diff --git a/Sources/ApiVideoPlayer/ApiVideoPlayerController.swift b/Sources/ApiVideoPlayer/ApiVideoPlayerController.swift index c09c469f..b08807e3 100644 --- a/Sources/ApiVideoPlayer/ApiVideoPlayerController.swift +++ b/Sources/ApiVideoPlayer/ApiVideoPlayerController.swift @@ -192,23 +192,28 @@ public class ApiVideoPlayerController: NSObject { self.avPlayer.play() } - public func replay() { - self.analytics? - .seek(from: Float(CMTimeGetSeconds(self.currentTime)), to: Float(CMTimeGetSeconds(CMTime.zero))) { result in - switch result { - case .success: break - case let .failure(error): print("analytics error on seek event: \(error)") + private func seekImpl(to time: CMTime, completion: @escaping (Bool) -> Void) { + self.avPlayer.seek(to: time, toleranceBefore: .zero, toleranceAfter: .zero) { finished in + self.analytics? + .seek( + from: Float(CMTimeGetSeconds(self.currentTime)), + to: Float(CMTimeGetSeconds(time)) + ) { result in + switch result { + case .success: break + case let .failure(error): print("analytics error on seek event: \(error)") + } } - } - self.avPlayer.seek(to: CMTime.zero) - self.play() - self.analytics?.resume { result in - switch result { - case .success: break - case let .failure(error): print("analytics error on resume event: \(error)") - } + completion(finished) } - for events in self.events { events.didReplay?() } + } + + public func replay() { + self.seekImpl(to: CMTime.zero, completion: { _ in + self.play() + for events in self.events { events.didReplay?() } + }) + } public func pause() { @@ -226,21 +231,18 @@ public class ApiVideoPlayerController: NSObject { public func seek(to: CMTime) { let from = self.currentTime - self.avPlayer.seek(to: to, toleranceBefore: .zero, toleranceAfter: .zero) - let calculatedTo = CMTime( - seconds: min(max(0.0, CMTimeGetSeconds(to)), CMTimeGetSeconds(duration)), - preferredTimescale: 1_000 - ) - self.analytics?.seek(from: from, to: calculatedTo) { result in - switch result { - case .success: break - case let .failure(error): print("analytics error seek: \(error)") + self.seekImpl(to: to, completion: { _ in + self.analytics?.seek(from: from, to: self.currentTime) { result in + switch result { + case .success: break + case let .failure(error): print("analytics error seek: \(error)") + } } - } - for events in self.events { - events.didSeek?(from, calculatedTo) - } + for events in self.events { + events.didSeek?(from, self.currentTime) + } + }) } public var videoOptions: VideoOptions? { From e89ece212e7abb528494b9363086f8b3bc2f4b7d Mon Sep 17 00:00:00 2001 From: Romain Petit Date: Tue, 22 Nov 2022 12:08:15 +0100 Subject: [PATCH 2/2] fix() analytics from and to, remove redundant seek analytics --- .../ApiVideoPlayer/ApiVideoPlayerController.swift | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/Sources/ApiVideoPlayer/ApiVideoPlayerController.swift b/Sources/ApiVideoPlayer/ApiVideoPlayerController.swift index b08807e3..7fd656eb 100644 --- a/Sources/ApiVideoPlayer/ApiVideoPlayerController.swift +++ b/Sources/ApiVideoPlayer/ApiVideoPlayerController.swift @@ -193,11 +193,12 @@ public class ApiVideoPlayerController: NSObject { } private func seekImpl(to time: CMTime, completion: @escaping (Bool) -> Void) { + let from = self.currentTime self.avPlayer.seek(to: time, toleranceBefore: .zero, toleranceAfter: .zero) { finished in self.analytics? .seek( - from: Float(CMTimeGetSeconds(self.currentTime)), - to: Float(CMTimeGetSeconds(time)) + from: Float(CMTimeGetSeconds(from)), + to: Float(CMTimeGetSeconds(self.currentTime)) ) { result in switch result { case .success: break @@ -232,13 +233,6 @@ public class ApiVideoPlayerController: NSObject { public func seek(to: CMTime) { let from = self.currentTime self.seekImpl(to: to, completion: { _ in - self.analytics?.seek(from: from, to: self.currentTime) { result in - switch result { - case .success: break - case let .failure(error): print("analytics error seek: \(error)") - } - } - for events in self.events { events.didSeek?(from, self.currentTime) }