1

From past developers, I got a large library, which is built on the basis of ExoPlayer, which allows you to listen to audio books.

Everything worked fine until the library itself had to update the version of ExoPlayer to a newer one (this was very important)

In short: there is a small interface that has a ProgressBar showing how many seconds the audio has already been playing. Here is the code for this:

private lateinit var mExoPlayer : ExoPlayer

private val compositeDisposable = CompositeDisposable()
private val currentTrackProgress = MutableLiveData<Long>()

private var currentTrackProgressDisposable : Disposable? = null
private val playbackProgressObservable : Observable<Long> =
    Observable.interval(1, TimeUnit.MILLISECONDS).map {
        mExoPlayer.currentPosition
    }.observeOn(AndroidSchedulers.mainThread())

////////////////////// So many code here
////////////////////// Somewhere there is a call to the "createExoPlayer()" function

private fun createExoPlayer() {
    mExoPlayer = ExoPlayer.Builder(this).build()

    mExoPlayer.addListener(object : Player.Listener {

        override fun onIsPlayingChanged(isPlaying : Boolean) {
            if (isPlaying) {
                currentTrackProgressDisposable = playbackProgressObservable.subscribeBy(
                    onError = { error ->
                        Log.d("error", error.toString())
                    },
                    onNext = { progress ->
                        currentTrackProgress.value = progress
                    }
                )
                compositeDisposable.add(currentTrackProgressDisposable!!)
            }
        }
    })

    mExoPlayer.playWhenReady = true
}

Before updating ExoPlayer version, everything worked fine, but after updating to the latest version, an error began to appear in onError:

java.lang.IllegalStateException: Player is accessed on the wrong thread.
Current thread: 'RxComputationThreadPool-1'
Expected thread: 'main'

What could be the reason for this and how to fix it? After all, I didn’t even update the RxJava2 version, but only updated ExoPlayer

onesector
  • 361
  • 5
  • 18

0 Answers0