Cancel radio job when starting a new radio

This commit is contained in:
vfsfitvnm
2022-06-27 21:59:01 +02:00
parent 697c6b6379
commit 85f89ec799

View File

@@ -500,6 +500,8 @@ class PlayerService : Service(), Player.Listener, PlaybackStatsListener.Callback
val sleepTimerMillisLeft: StateFlow<Long?>? val sleepTimerMillisLeft: StateFlow<Long?>?
get() = timerJob?.millisLeft get() = timerJob?.millisLeft
private var radioJob: Job? = null
fun startSleepTimer(delayMillis: Long) { fun startSleepTimer(delayMillis: Long) {
timerJob?.cancel() timerJob?.cancel()
@@ -532,6 +534,7 @@ class PlayerService : Service(), Player.Listener, PlaybackStatsListener.Callback
startRadio(endpoint= endpoint, justAdd = false) startRadio(endpoint= endpoint, justAdd = false)
private fun startRadio(endpoint: NavigationEndpoint.Endpoint.Watch?, justAdd: Boolean) { private fun startRadio(endpoint: NavigationEndpoint.Endpoint.Watch?, justAdd: Boolean) {
radioJob?.cancel()
radio = null radio = null
YoutubePlayer.Radio( YoutubePlayer.Radio(
endpoint?.videoId, endpoint?.videoId,
@@ -539,7 +542,7 @@ class PlayerService : Service(), Player.Listener, PlaybackStatsListener.Callback
endpoint?.playlistSetVideoId, endpoint?.playlistSetVideoId,
endpoint?.params endpoint?.params
).let { ).let {
coroutineScope.launch(Dispatchers.Main) { radioJob = coroutineScope.launch(Dispatchers.Main) {
if (justAdd) { if (justAdd) {
player.addMediaItems(it.process().drop(1)) player.addMediaItems(it.process().drop(1))
} else { } else {
@@ -551,6 +554,7 @@ class PlayerService : Service(), Player.Listener, PlaybackStatsListener.Callback
} }
fun stopRadio() { fun stopRadio() {
radioJob?.cancel()
radio = null radio = null
} }
} }