refactor: improve player lifecycle
This commit is contained in:
+19
-8
@@ -82,15 +82,23 @@ internal class PlaybackRepositoryImpl(
|
||||
|
||||
@OptIn(ExperimentalCoroutinesApi::class)
|
||||
private val currentPosition: Flow<Duration> = playbackState.flatMapLatest { state ->
|
||||
if (state in listOf(PlaybackState.PLAYING, PlaybackState.PAUSED)) {
|
||||
flow {
|
||||
while (true) {
|
||||
emit(audioPlayer.currentPosition)
|
||||
delay(POSITION_POLL_INTERVAL)
|
||||
when (state) {
|
||||
in listOf(PlaybackState.PLAYING, PlaybackState.PAUSED) -> {
|
||||
flow {
|
||||
while (true) {
|
||||
emit(audioPlayer.currentPosition)
|
||||
delay(POSITION_POLL_INTERVAL)
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
flowOf(audioPlayer.currentPosition)
|
||||
|
||||
PlaybackState.IDLE -> {
|
||||
flowOf(Duration.ZERO)
|
||||
}
|
||||
|
||||
else -> {
|
||||
flowOf(audioPlayer.currentPosition)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -304,7 +312,9 @@ internal class PlaybackRepositoryImpl(
|
||||
)
|
||||
|
||||
override suspend fun stop() {
|
||||
cancelRunningBreakJob()
|
||||
breakJob?.cancel()
|
||||
isBreaking.value = false
|
||||
subtitle.value = Subtitle(emptyList())
|
||||
audioPlayer.stop()
|
||||
}
|
||||
|
||||
@@ -312,6 +322,7 @@ internal class PlaybackRepositoryImpl(
|
||||
breakJob?.cancel()
|
||||
isBreaking.value = false
|
||||
audioPlayer.release()
|
||||
subtitle.value = Subtitle(emptyList())
|
||||
}
|
||||
|
||||
private suspend fun cancelRunningBreakJob() {
|
||||
|
||||
Reference in New Issue
Block a user