0

I am working on an Android application which does lots of concurrencies and RxJava treading in the background and it is possible to not want to play a music which is in the async preparation phase for some reason:

player.prepareAsync()

and

override fun onPrepared(mp: MediaPlayer?) {
    isPreparing = false

    if (playAfterPreparing) {
        //start playback
        mp?.start()
    } else {
        // don't play and keep it prepare for later!!!
    }

    updateNotification()
}

but the problem is after preparation you should play(!) because: Media Player called in state 0, error (-38,0)

I even tried to start and pause one after another in preparation:

override fun onPrepared(mp: MediaPlayer?) {
    Timber.i("onPrepared")
    Timber.i("playAfterPreparing: $playAfterPreparing")

    isPreparing = false

    //start playback
    mp?.start()

    if (!playAfterPreparing) {
        mp?.pause()
    }

    updateNotification()
}

but it didn't work cause it rendered to the same issue.

I also know how MediaPlayer state machine is working, but don't see anything in this situation in documents: https://developer.android.com/reference/android/media/MediaPlayer.html

any solution for such a situation?

Mohsen Mirhoseini
  • 8,454
  • 5
  • 34
  • 59

1 Answers1

0

Thanks to @VladyslavMatviienko comments, I suspected that I am doing something wrong somewhere else and he was correct. According to Android MediaPlayer document:

Android MediaPlayer State Diagram

It is possible to stay in Prepared state, but it is not possible to call any method namely pause() and that was my fault. In Prepared state You can only call start(), seekTo() or stop().

Hope this answer comes handy for those who might face this issue later.

Mohsen Mirhoseini
  • 8,454
  • 5
  • 34
  • 59