From 37b716982e3513822f47c721b1f107059af3eb2c Mon Sep 17 00:00:00 2001 From: Romain Petit Date: Wed, 23 Nov 2022 10:00:57 +0100 Subject: [PATCH 1/3] feat() add didReady event, call didPrepare before setting avplayer --- .../ApiVideoPlayerController.swift | 17 ++++++++--------- Sources/ApiVideoPlayer/PlayerEvents.swift | 3 +++ 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/Sources/ApiVideoPlayer/ApiVideoPlayerController.swift b/Sources/ApiVideoPlayer/ApiVideoPlayerController.swift index 7fd656eb..eba82070 100644 --- a/Sources/ApiVideoPlayer/ApiVideoPlayerController.swift +++ b/Sources/ApiVideoPlayer/ApiVideoPlayerController.swift @@ -85,9 +85,6 @@ public class ApiVideoPlayerController: NSObject { self.playerManifest = try JSONDecoder().decode(PlayerManifest.self, from: data) self.setUpAnalytics(url: self.playerManifest.video.src) try self.setUpPlayer(self.playerManifest.video.src) - for event in self.events { - event.didPrepare?() - } completion(nil) } catch { completion(error) @@ -107,9 +104,6 @@ public class ApiVideoPlayerController: NSObject { } do { try self.setUpPlayer(mp4) - for event in self.events { - event.didPrepare?() - } } catch { self.notifyError(error: error) } @@ -117,6 +111,9 @@ public class ApiVideoPlayerController: NSObject { private func setUpPlayer(_ url: String) throws { if let url = URL(string: url) { + for event in self.events { + event.didPrepare?() + } let item = AVPlayerItem(url: url) self.avPlayer.currentItem?.removeObserver(self, forKeyPath: "status", context: nil) self.avPlayer.replaceCurrentItem(with: item) @@ -396,9 +393,11 @@ public class ApiVideoPlayerController: NSObject { } } - private func doAutoplay() { - + private func doReadyToPlay() { if self.avPlayer.currentItem?.status == .readyToPlay { + for events in self.events { + events.didReady?() + } if self.autoplay { self.play() } @@ -478,7 +477,7 @@ public class ApiVideoPlayerController: NSObject { ) { if keyPath == "status" { self.doFallbackOnFailed() - self.doAutoplay() + self.doReadyToPlay() } if keyPath == "timeControlStatus" { guard let change = change else { return } diff --git a/Sources/ApiVideoPlayer/PlayerEvents.swift b/Sources/ApiVideoPlayer/PlayerEvents.swift index c5546936..fc9a54ab 100644 --- a/Sources/ApiVideoPlayer/PlayerEvents.swift +++ b/Sources/ApiVideoPlayer/PlayerEvents.swift @@ -2,6 +2,7 @@ import AVFoundation import Foundation public class PlayerEvents { public var didPrepare: (() -> Void)? + public var didReady: (() -> Void)? public var didPause: (() -> Void)? public var didPlay: (() -> Void)? public var didReplay: (() -> Void)? @@ -16,6 +17,7 @@ public class PlayerEvents { public init( didPrepare: (() -> Void)? = nil, + didReady: (() -> Void)? = nil, didPause: (() -> Void)? = nil, didPlay: (() -> Void)? = nil, didReplay: (() -> Void)? = nil, @@ -29,6 +31,7 @@ public class PlayerEvents { didVideoSizeChanged: ((CGSize) -> Void)? = nil ) { self.didPrepare = didPrepare + self.didReady = didReady self.didPause = didPause self.didPlay = didPlay self.didReplay = didReplay From df71065c899650c22cb483f44d4bc37cfa7d28af Mon Sep 17 00:00:00 2001 From: Romain Petit Date: Wed, 23 Nov 2022 10:01:19 +0100 Subject: [PATCH 2/3] chore() add doc about events --- Sources/ApiVideoPlayer/PlayerEvents.swift | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/Sources/ApiVideoPlayer/PlayerEvents.swift b/Sources/ApiVideoPlayer/PlayerEvents.swift index fc9a54ab..a755405a 100644 --- a/Sources/ApiVideoPlayer/PlayerEvents.swift +++ b/Sources/ApiVideoPlayer/PlayerEvents.swift @@ -1,18 +1,31 @@ import AVFoundation import Foundation public class PlayerEvents { + /// Events called when the player is preparing for a video public var didPrepare: (() -> Void)? + /// Events called when the player is ready to play video public var didReady: (() -> Void)? + /// Events called when the video is paused public var didPause: (() -> Void)? + /// Events called when the video is playing public var didPlay: (() -> Void)? + /// Events called when the video is replayed public var didReplay: (() -> Void)? + /// Events called when the player is muted public var didMute: (() -> Void)? + /// Events called when the player is unmuted public var didUnMute: (() -> Void)? + /// Events called when the video is replayed in a loop public var didLoop: (() -> Void)? + /// Events called when the player volume is changed public var didSetVolume: ((_ volume: Float) -> Void)? + /// Events called when the player is seeking in the video public var didSeek: ((_ from: CMTime, _ to: CMTime) -> Void)? + /// Events called when the video ended public var didEnd: (() -> Void)? + /// Events called when there is an error with the player or video public var didError: ((_ error: Error) -> Void)? + /// Events called when the size of the video changed public var didVideoSizeChanged: ((_ size: CGSize) -> Void)? public init( From f88929170bc723666232f63991a1550219b5c90b Mon Sep 17 00:00:00 2001 From: Romain Petit Date: Wed, 23 Nov 2022 10:01:55 +0100 Subject: [PATCH 3/3] feaat() add didPrepare and didReady in sample app --- .../ExampleUIKit/ExampleUIkit/PlayerViewController.swift | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Examples/ExampleUIKit/ExampleUIkit/PlayerViewController.swift b/Examples/ExampleUIKit/ExampleUIkit/PlayerViewController.swift index 1d69aa27..9a66986a 100644 --- a/Examples/ExampleUIKit/ExampleUIkit/PlayerViewController.swift +++ b/Examples/ExampleUIKit/ExampleUIkit/PlayerViewController.swift @@ -11,6 +11,12 @@ class PlayerViewController: UIViewController { let playerView: ApiVideoPlayerView = { let events = PlayerEvents( + didPrepare: { () in + print("didPrepare") + }, + didReady: { () in + print("didReady") + }, didPause: { () in print("paused") },