refactor: improve player lifecycle
This commit is contained in:
+2
@@ -79,9 +79,11 @@ internal class AndroidAudioPlayer(
|
|||||||
|
|
||||||
override fun stop() {
|
override fun stop() {
|
||||||
player.stop()
|
player.stop()
|
||||||
|
_playbackState.value = PlaybackState.IDLE
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun release() {
|
override fun release() {
|
||||||
player.release()
|
player.release()
|
||||||
|
_playbackState.value = PlaybackState.IDLE
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+14
-3
@@ -82,17 +82,25 @@ internal class PlaybackRepositoryImpl(
|
|||||||
|
|
||||||
@OptIn(ExperimentalCoroutinesApi::class)
|
@OptIn(ExperimentalCoroutinesApi::class)
|
||||||
private val currentPosition: Flow<Duration> = playbackState.flatMapLatest { state ->
|
private val currentPosition: Flow<Duration> = playbackState.flatMapLatest { state ->
|
||||||
if (state in listOf(PlaybackState.PLAYING, PlaybackState.PAUSED)) {
|
when (state) {
|
||||||
|
in listOf(PlaybackState.PLAYING, PlaybackState.PAUSED) -> {
|
||||||
flow {
|
flow {
|
||||||
while (true) {
|
while (true) {
|
||||||
emit(audioPlayer.currentPosition)
|
emit(audioPlayer.currentPosition)
|
||||||
delay(POSITION_POLL_INTERVAL)
|
delay(POSITION_POLL_INTERVAL)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
}
|
||||||
|
|
||||||
|
PlaybackState.IDLE -> {
|
||||||
|
flowOf(Duration.ZERO)
|
||||||
|
}
|
||||||
|
|
||||||
|
else -> {
|
||||||
flowOf(audioPlayer.currentPosition)
|
flowOf(audioPlayer.currentPosition)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private val playbackTiming = currentPosition.map { currentPosition ->
|
private val playbackTiming = currentPosition.map { currentPosition ->
|
||||||
PlaybackTiming(
|
PlaybackTiming(
|
||||||
@@ -304,7 +312,9 @@ internal class PlaybackRepositoryImpl(
|
|||||||
)
|
)
|
||||||
|
|
||||||
override suspend fun stop() {
|
override suspend fun stop() {
|
||||||
cancelRunningBreakJob()
|
breakJob?.cancel()
|
||||||
|
isBreaking.value = false
|
||||||
|
subtitle.value = Subtitle(emptyList())
|
||||||
audioPlayer.stop()
|
audioPlayer.stop()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -312,6 +322,7 @@ internal class PlaybackRepositoryImpl(
|
|||||||
breakJob?.cancel()
|
breakJob?.cancel()
|
||||||
isBreaking.value = false
|
isBreaking.value = false
|
||||||
audioPlayer.release()
|
audioPlayer.release()
|
||||||
|
subtitle.value = Subtitle(emptyList())
|
||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun cancelRunningBreakJob() {
|
private suspend fun cancelRunningBreakJob() {
|
||||||
|
|||||||
+2
-1
@@ -59,6 +59,8 @@ class PlayerViewModel(
|
|||||||
|
|
||||||
fun loadRecord() {
|
fun loadRecord() {
|
||||||
viewModelScope.launch {
|
viewModelScope.launch {
|
||||||
|
playbackRepository.stop()
|
||||||
|
|
||||||
logger.debug("Loading record $recordId")
|
logger.debug("Loading record $recordId")
|
||||||
val alreadyLoadedRecord = record.value
|
val alreadyLoadedRecord = record.value
|
||||||
if (alreadyLoadedRecord != null) {
|
if (alreadyLoadedRecord != null) {
|
||||||
@@ -177,6 +179,5 @@ class PlayerViewModel(
|
|||||||
override fun onCleared() {
|
override fun onCleared() {
|
||||||
super.onCleared()
|
super.onCleared()
|
||||||
audioSessionManager.deactivate()
|
audioSessionManager.deactivate()
|
||||||
playbackRepository.release()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user