mirror of
https://github.com/sudoxnym/audiobookshelf-atv.git
synced 2026-04-14 19:46:30 +00:00
Add:Sync local progress to server natively #74
This commit is contained in:
parent
1a555eab63
commit
16da0c909f
2 changed files with 37 additions and 2 deletions
|
|
@ -114,9 +114,36 @@ class AudiobookProgressSyncer constructor(playerNotificationService:PlayerNotifi
|
|||
} else if (listeningStreamId == "download") {
|
||||
// TODO: Save downloaded audiobook progress & send to server if connected
|
||||
Log.d(tag, "ListeningTimer: Is listening download")
|
||||
|
||||
// Send sync data only for local books
|
||||
var syncData: JSObject = JSObject()
|
||||
var duration = playerNotificationService.getAudiobookDuration() / 1000
|
||||
var currentTime = playerNotificationService.getCurrentTime() / 1000
|
||||
syncData.put("totalDuration", duration)
|
||||
syncData.put("currentTime", currentTime)
|
||||
syncData.put("progress", if (duration > 0) (currentTime / duration) else 0)
|
||||
syncData.put("isRead", false)
|
||||
syncData.put("lastUpdate", System.currentTimeMillis())
|
||||
syncData.put("audiobookId", listeningBookId)
|
||||
sendLocalSyncData(syncData) {
|
||||
Log.d(tag, "Local sync done")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun sendLocalSyncData(payload:JSObject, cb: (() -> Unit)) {
|
||||
var serverUrl = playerNotificationService.getServerUrl()
|
||||
var token = playerNotificationService.getUserToken()
|
||||
|
||||
if (serverUrl == "" || token == "") {
|
||||
return
|
||||
}
|
||||
|
||||
Log.d(tag, "Sync Local $serverUrl | $token")
|
||||
var url = "$serverUrl/api/syncLocal"
|
||||
sendServerRequest(url, token, payload, cb)
|
||||
}
|
||||
|
||||
fun sendStreamSyncData(payload:JSObject, cb: (() -> Unit)) {
|
||||
var serverUrl = playerNotificationService.getServerUrl()
|
||||
var token = playerNotificationService.getUserToken()
|
||||
|
|
@ -127,7 +154,10 @@ class AudiobookProgressSyncer constructor(playerNotificationService:PlayerNotifi
|
|||
|
||||
Log.d(tag, "Sync Stream $serverUrl | $token")
|
||||
var url = "$serverUrl/api/syncStream"
|
||||
sendServerRequest(url, token, payload, cb)
|
||||
}
|
||||
|
||||
fun sendServerRequest(url:String, token:String, payload:JSObject, cb: () -> Unit) {
|
||||
val mediaType = "application/json; charset=utf-8".toMediaType()
|
||||
val requestBody = payload.toString().toRequestBody(mediaType)
|
||||
val request = Request.Builder().post(requestBody)
|
||||
|
|
|
|||
|
|
@ -648,8 +648,7 @@ class PlayerNotificationService : MediaBrowserServiceCompat() {
|
|||
Log.d(tag, "Playing ${getCurrentBookTitle()} | ${currentPlayer.mediaMetadata.title} | ${currentPlayer.mediaMetadata.displayTitle}")
|
||||
if (player.isPlaying) {
|
||||
audiobookProgressSyncer.start()
|
||||
}
|
||||
if (!player.isPlaying && audiobookProgressSyncer.listeningTimerRunning) {
|
||||
} else {
|
||||
audiobookProgressSyncer.stop()
|
||||
}
|
||||
|
||||
|
|
@ -784,6 +783,12 @@ class PlayerNotificationService : MediaBrowserServiceCompat() {
|
|||
return currentAudiobookStreamData?.id
|
||||
}
|
||||
|
||||
// The duration stored on the audiobook
|
||||
fun getAudiobookDuration() : Long {
|
||||
if (currentAudiobookStreamData == null) return 0L
|
||||
return currentAudiobookStreamData!!.duration
|
||||
}
|
||||
|
||||
fun getServerUrl(): String {
|
||||
return audiobookManager.serverUrl
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue