Fix progress syncing

This commit is contained in:
ronaldheft 2022-08-16 21:14:33 -04:00
parent 6aa0f2253b
commit 10f2da9e90
3 changed files with 16 additions and 6 deletions

View file

@ -130,7 +130,9 @@ public class AbsAudioPlayer: CAPPlugin {
@objc func sendMetadata() {
self.notifyListeners("onPlayingUpdate", data: [ "value": !PlayerHandler.paused ])
self.notifyListeners("onMetadata", data: PlayerHandler.getMetdata())
if let metadata = PlayerHandler.getMetdata() {
self.notifyListeners("onMetadata", data: metadata)
}
}
@objc func sendPlaybackClosedEvent() {
self.notifyListeners("onPlaybackClosed", data: [ "value": true ])

View file

@ -107,6 +107,10 @@ class AudioPlayer: NSObject {
NotificationCenter.default.post(name: NSNotification.Name(PlayerEvents.closed.rawValue), object: nil)
}
func isInitialized() -> Bool {
return self.status != -1
}
func getItemIndexForTime(time:Double) -> Int {
let playbackSession = Database.shared.getPlaybackSession(id: self.sessionId)!
for index in 0..<self.allPlayerItems.count {

View file

@ -157,16 +157,19 @@ class PlayerHandler {
player?.seek(amount, from: "handler")
}
public static func getMetdata() -> [String: Any] {
public static func getMetdata() -> [String: Any]? {
guard let player = player else { return nil }
guard player.isInitialized() else { return nil }
DispatchQueue.main.async {
syncProgress()
}
return [
"duration": player?.getDuration() ?? 0,
"currentTime": player?.getCurrentTime() ?? 0,
"duration": player.getDuration(),
"currentTime": player.getCurrentTime(),
"playerState": !paused,
"currentRate": player?.rate ?? 0,
"currentRate": player.rate,
]
}
@ -199,6 +202,7 @@ class PlayerHandler {
public static func syncProgress() {
guard let player = player else { return }
guard player.isInitialized() else { return }
guard let session = getPlaybackSession() else { return }
// Get current time
@ -254,7 +258,7 @@ class PlayerHandler {
guard Connectivity.isConnectedToInternet else { return }
DispatchQueue.global(qos: .utility).async {
let realm = try! Realm()
for session in realm.objects(PlaybackSession.self) {
for session in realm.objects(PlaybackSession.self).where({ $0.serverConnectionConfigId == Store.serverConfig?.id }) {
NSLog("Sending sessionId(\(session.id)) to server")
let sessionRef = ThreadSafeReference(to: session)
ApiClient.reportLocalPlaybackProgress(session.freeze()) { success in