Skip to content

Commit

Permalink
update: player position saver
Browse files Browse the repository at this point in the history
  • Loading branch information
muedsa committed Nov 8, 2023
1 parent c739f63 commit 863aa0f
Showing 1 changed file with 26 additions and 18 deletions.
44 changes: 26 additions & 18 deletions app/src/main/kotlin/com/muedsa/agetv/viewmodel/PlaybackViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -84,27 +84,35 @@ class PlaybackViewModel @Inject constructor(
episodeTitle: String,
exoPlayer: ExoPlayer,
stopState: State<Boolean>
) {
val id = "$aid:$episodeTitle"
synchronized(_saverIdSet) {
if (!_saverIdSet.contains(id)) {
_saverIdSet.add(id)
saverRunning(id, aid, episodeTitle, exoPlayer, stopState)
}
}
}

private fun saverRunning(
id: String,
aid: Int,
episodeTitle: String,
exoPlayer: ExoPlayer,
stopState: State<Boolean>
) {
viewModelScope.launch(Dispatchers.Unconfined) {
val id = "$aid:$episodeTitle"
synchronized(_saverIdSet) {
if (!_saverIdSet.contains(id)) {
_saverIdSet.add(id)
viewModelScope.launch(Dispatchers.Unconfined) {
LogUtil.d("[Player position saver-${id}] running for $aid-$episodeTitle")
while (!stopState.value) {
delay(15 * 1000)
val pos = withContext(Dispatchers.Main) {
exoPlayer.currentPosition
}
LogUtil.d("[Player position saver-${id}] save pos: $pos for $aid-$episodeTitle")
}
LogUtil.d("[Player position saver-${id}] stop for $aid-$episodeTitle")
synchronized(_saverIdSet) {
_saverIdSet.remove(id)
}
}
LogUtil.d("[Player position saver-${id}] running for $aid-$episodeTitle")
while (!stopState.value) {
delay(15 * 1000)
val pos = withContext(Dispatchers.Main) {
exoPlayer.currentPosition
}
LogUtil.d("[Player position saver-${id}] save pos: $pos for $aid-$episodeTitle")
}
LogUtil.d("[Player position saver-${id}] stop for $aid-$episodeTitle")
synchronized(_saverIdSet) {
_saverIdSet.remove(id)
}
}
}
Expand Down

0 comments on commit 863aa0f

Please sign in to comment.