Skip to content

Commit

Permalink
Merge pull request #13 from apivideo/feat/didReady
Browse files Browse the repository at this point in the history
Feat/didready
  • Loading branch information
RomainPetit1 authored Nov 23, 2022
2 parents ba2daba + f889291 commit 84161ee
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
6 changes: 6 additions & 0 deletions Examples/ExampleUIKit/ExampleUIkit/PlayerViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")
},
Expand Down
17 changes: 8 additions & 9 deletions Sources/ApiVideoPlayer/ApiVideoPlayerController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -107,16 +104,16 @@ public class ApiVideoPlayerController: NSObject {
}
do {
try self.setUpPlayer(mp4)
for event in self.events {
event.didPrepare?()
}
} catch {
self.notifyError(error: error)
}
}

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)
Expand Down Expand Up @@ -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()
}
Expand Down Expand Up @@ -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 }
Expand Down
16 changes: 16 additions & 0 deletions Sources/ApiVideoPlayer/PlayerEvents.swift
Original file line number Diff line number Diff line change
@@ -1,21 +1,36 @@
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(
didPrepare: (() -> Void)? = nil,
didReady: (() -> Void)? = nil,
didPause: (() -> Void)? = nil,
didPlay: (() -> Void)? = nil,
didReplay: (() -> Void)? = nil,
Expand All @@ -29,6 +44,7 @@ public class PlayerEvents {
didVideoSizeChanged: ((CGSize) -> Void)? = nil
) {
self.didPrepare = didPrepare
self.didReady = didReady
self.didPause = didPause
self.didPlay = didPlay
self.didReplay = didReplay
Expand Down

0 comments on commit 84161ee

Please sign in to comment.