Fix an exception when removeIncompleteTracksFromPlaylist() could be called on the wrong thread.

This commit is contained in:
birdbird 2023-07-24 18:57:29 +00:00 committed by tzugen
parent 1fa5f4c2f8
commit 4f55a2a4a5
No known key found for this signature in database
GPG Key ID: 61E9C34BC10EC930
2 changed files with 25 additions and 27 deletions

View File

@ -181,19 +181,22 @@ 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()
rxBusSubscription += RxBus.activeServerChangingObservable
// All interaction with the Media3 needs to happen on the main thread
.subscribeOn(RxBus.mainThread())
.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()
}
}
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()
}
}
rxBusSubscription += RxBus.activeServerChangedObservable.subscribe {
val jukebox = activeServerProvider.getActiveServer().jukeboxByDefault
@ -204,19 +207,19 @@ class MediaPlayerManager(
isJukeboxEnabled = jukebox
}
rxBusSubscription += RxBus.throttledPlaylistObservable.subscribe {
// Even though Rx should launch on the main thread it doesn't always :(
mainScope.launch {
rxBusSubscription += RxBus.throttledPlaylistObservable
// All interaction with the Media3 needs to happen on the main thread
.subscribeOn(RxBus.mainThread())
.subscribe {
serializeCurrentSession()
}
}
rxBusSubscription += RxBus.throttledPlayerStateObservable.subscribe {
// Even though Rx should launch on the main thread it doesn't always :(
mainScope.launch {
rxBusSubscription += RxBus.throttledPlayerStateObservable
// All interaction with the Media3 needs to happen on the main thread
.subscribeOn(RxBus.mainThread())
.subscribe {
serializeCurrentSession()
}
}
rxBusSubscription += RxBus.shutdownCommandObservable.subscribe {
clear(false)

View File

@ -1,8 +1,8 @@
package org.moire.ultrasonic.service
import android.os.Looper
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
import io.reactivex.rxjava3.core.Observable
import io.reactivex.rxjava3.core.Scheduler
import io.reactivex.rxjava3.disposables.CompositeDisposable
import io.reactivex.rxjava3.disposables.Disposable
import io.reactivex.rxjava3.subjects.PublishSubject
@ -12,14 +12,9 @@ import org.moire.ultrasonic.domain.Track
class RxBus {
/*
* TODO: mainThread() seems to be not equal to the "normal" main Thread, so it causes
* a lot of often unnecessary thread switching. It looks like observeOn can actually
* be removed in many cases
*/
companion object {
private fun mainThread() = AndroidSchedulers.from(Looper.getMainLooper())
fun mainThread(): Scheduler = AndroidSchedulers.mainThread()
val shufflePlayPublisher: PublishSubject<Boolean> =
PublishSubject.create()