From 8d43b935ab4d9bb0e5e2964f2371e7f0688be1aa Mon Sep 17 00:00:00 2001 From: tzugen Date: Thu, 13 Jul 2023 13:19:03 +0200 Subject: [PATCH] Fix an exception when removeIncompleteTracksFromPlaylist() could be called on the wrong thread. --- .../ultrasonic/service/MediaPlayerManager.kt | 23 +++++++++++-------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/service/MediaPlayerManager.kt b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/service/MediaPlayerManager.kt index d1ffd067..cbeb167d 100644 --- a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/service/MediaPlayerManager.kt +++ b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/service/MediaPlayerManager.kt @@ -182,16 +182,19 @@ class MediaPlayerManager( createMediaController(onCreated) rxBusSubscription += RxBus.activeServerChangingObservable.subscribe { oldServer -> - if (oldServer != OFFLINE_DB_ID) { - // When the server changes, the playlist can retain the downloaded songs. - // Incomplete songs should be removed as the new server won't recognise them. - removeIncompleteTracksFromPlaylist() - DownloadService.requestStop() - } - if (controller is JukeboxMediaPlayer) { - // When the server changes, the Jukebox should be released. - // The new server won't understand the jukebox requests of the old one. - switchToLocalPlayer() + // Even though Rx should launch on the main thread it doesn't always :( + mainScope.launch { + if (oldServer != OFFLINE_DB_ID) { + // When the server changes, the playlist can retain the downloaded songs. + // Incomplete songs should be removed as the new server won't recognise them. + removeIncompleteTracksFromPlaylist() + DownloadService.requestStop() + } + if (controller is JukeboxMediaPlayer) { + // When the server changes, the Jukebox should be released. + // The new server won't understand the jukebox requests of the old one. + switchToLocalPlayer() + } } }