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) createMediaController(onCreated)
rxBusSubscription += RxBus.activeServerChangingObservable.subscribe { oldServer -> rxBusSubscription += RxBus.activeServerChangingObservable
if (oldServer != OFFLINE_DB_ID) { // All interaction with the Media3 needs to happen on the main thread
// When the server changes, the playlist can retain the downloaded songs. .subscribeOn(RxBus.mainThread())
// Incomplete songs should be removed as the new server won't recognise them. .subscribe { oldServer ->
removeIncompleteTracksFromPlaylist() if (oldServer != OFFLINE_DB_ID) {
DownloadService.requestStop() // 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 { rxBusSubscription += RxBus.activeServerChangedObservable.subscribe {
val jukebox = activeServerProvider.getActiveServer().jukeboxByDefault val jukebox = activeServerProvider.getActiveServer().jukeboxByDefault
@ -204,19 +207,19 @@ class MediaPlayerManager(
isJukeboxEnabled = jukebox isJukeboxEnabled = jukebox
} }
rxBusSubscription += RxBus.throttledPlaylistObservable.subscribe { rxBusSubscription += RxBus.throttledPlaylistObservable
// Even though Rx should launch on the main thread it doesn't always :( // All interaction with the Media3 needs to happen on the main thread
mainScope.launch { .subscribeOn(RxBus.mainThread())
.subscribe {
serializeCurrentSession() serializeCurrentSession()
} }
}
rxBusSubscription += RxBus.throttledPlayerStateObservable.subscribe { rxBusSubscription += RxBus.throttledPlayerStateObservable
// Even though Rx should launch on the main thread it doesn't always :( // All interaction with the Media3 needs to happen on the main thread
mainScope.launch { .subscribeOn(RxBus.mainThread())
.subscribe {
serializeCurrentSession() serializeCurrentSession()
} }
}
rxBusSubscription += RxBus.shutdownCommandObservable.subscribe { rxBusSubscription += RxBus.shutdownCommandObservable.subscribe {
clear(false) clear(false)

View File

@ -1,8 +1,8 @@
package org.moire.ultrasonic.service package org.moire.ultrasonic.service
import android.os.Looper
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
import io.reactivex.rxjava3.core.Observable import io.reactivex.rxjava3.core.Observable
import io.reactivex.rxjava3.core.Scheduler
import io.reactivex.rxjava3.disposables.CompositeDisposable import io.reactivex.rxjava3.disposables.CompositeDisposable
import io.reactivex.rxjava3.disposables.Disposable import io.reactivex.rxjava3.disposables.Disposable
import io.reactivex.rxjava3.subjects.PublishSubject import io.reactivex.rxjava3.subjects.PublishSubject
@ -12,14 +12,9 @@ import org.moire.ultrasonic.domain.Track
class RxBus { 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 { companion object {
private fun mainThread() = AndroidSchedulers.from(Looper.getMainLooper()) fun mainThread(): Scheduler = AndroidSchedulers.mainThread()
val shufflePlayPublisher: PublishSubject<Boolean> = val shufflePlayPublisher: PublishSubject<Boolean> =
PublishSubject.create() PublishSubject.create()