Attempting to use this Media3 librarywhich claimed...
# compose
e
Attempting to use this Media3 librarywhich claimed it was simple but is not. This extremely problematic error is causing me some grief and I am hoping someone can help me with this. More details of pain in the thread
Copy code
java.lang.IllegalStateException: MediaController method is called from a wrong thread. See javadoc of MediaController for details.
                                                                                                    	at androidx.media3.common.util.Assertions.checkState(Assertions.java:100)
                                                                                                    	at androidx.media3.session.MediaController.verifyApplicationThread(MediaController.java:1981)
I wrote this class for my MediaController
Copy code
class Player(private val context: Context ) : AudioPlayer {

    private var file: String? = null

     private var player: MediaController? = null

    var builtPlayer = false

init {
    buildPlayer()
}

    private fun buildPlayer() {

            val factory =  runBlocking {
                MediaController.Builder(
                    context,
                    SessionToken(
                        context,
                        ComponentName(context, PlaybackService::class.java)
                    )
                ).buildAsync()
            }

            factory.addListener(
                {
                player = factory.let {
                    if (it.isDone) {
                        println("Is Done")
                        builtPlayer = true
                        val poodoo = it.get()
                        println("THE PLAYER IS NOW: " + poodoo)
                        poodoo
                    } else {
                      null
                    }
                }

            },
                ContextCompat.getMainExecutor(context)
            )

        println(("PLAYER:" + this.player) )
    }


    override fun loadedFile(): String? = file

    private fun onApplicationThread(block: () -> Unit) {
        player?.applicationLooper?.run {
            block()
        }
    }

    override fun load(filePath: String) {



        player?.setMediaItem(MediaItem.fromUri(filePath))
        if (file == null) {
            onApplicationThread { player?.prepare() }
        }
        file = filePath

    }

    override fun play() {
        onApplicationThread { player?.play() }
    }

    override fun stop() {
        player?.stop()
    }

    override fun pause() {
        onApplicationThread { player?.pause() }
    }
}
This is how I call the methods for playing/pausing/stopping/etc..
Copy code
private fun parallel(func: ()->Unit) =  GlobalScope.launch { func() }


    @Composable
    override fun <Q> doAction(input: Song, actionCode: Int, actionData: Q?) {
        when(actionCode.asEnumValue<Actions>()) {

            Actions.LOAD -> {
                actionData
                    .verifiedCast<Q, String>().let {
                        audioPlayer.load(it)
                    }
            }
            // Verification is expected to be handled by individual players, only forward user intent
            Actions.PLAY -> {
                parallel(audioPlayer::play)
            }
            Actions.PAUSE -> {
                parallel(audioPlayer::pause)
            }
            Actions.STOP -> {
                parallel(audioPlayer::stop)
            }
        }
    }
I think the problem may be the GlobalScope... Idk if i can get an executor for it or something but I need that so that the buttons dont hang