Exceptions are not yet supported across processes

In my app,I’m implementing both TtsSpeaker.Listener and coroutine, and when I’m calling the speek function, I get the above error, as the app not able to execute the onTtsSpoken function, my code is as:

class MainActivity : AppCompatActivity(), TtsSpeaker.Listener  {
    private val scope = MainScope()
    override fun onCreate(savedInstanceState: Bundle?) {
        btnCamera.setOnClickListener { 
            startCamera()
        }
    }

   private fun startCamera() { 
       
   }
   override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { 
           scope.launch {
                    val detectedFaces = FaceDetection.detectFaces(bitmap)
                    println("Detected Faces = $detectedFaces")
                    Toast.makeText(
                        this@MainActivity, "Detected Faces = $detectedFaces",
                        Toast.LENGTH_SHORT
                    ).show()
                    runOnUiThread {
                        tts!!.say("Detected Faces are: $detectedFaces")
                    }
         }
    }

   override fun onTtsSpoken() { }
}

The error I get is:

I/TtsSpeaker: onDone
E/JavaBinder: *** Uncaught remote exception!  (Exceptions are not yet supported across processes.)
    kotlin.NotImplementedError: An operation is not implemented.
        at hasan.tts_mobile.MainActivity.onTtsSpoken(MainActivity.kt:195)
        at hasan.tts_mobile.TtsSpeaker$onInit$1.onDone(TtsSpeaker.kt:34)
        at android.speech.tts.TextToSpeech$Connection$1.onSuccess(TextToSpeech.java:2131)
        at android.speech.tts.ITextToSpeechCallback$Stub.onTransact(ITextToSpeechCallback.java:202)
        at android.os.Binder.execTransactInternal(Binder.java:1021)
        at android.os.Binder.execTransact(Binder.java:994)
E/AndroidRuntime: FATAL EXCEPTION: Binder:8566_1
    Process: hasan.tts_mobile, PID: 8566
    kotlin.NotImplementedError: An operation is not implemented.
        at hasan.tts_mobile.MainActivity.onTtsSpoken(MainActivity.kt:195)
        at hasan.tts_mobile.TtsSpeaker$onInit$1.onDone(TtsSpeaker.kt:34)
        at android.speech.tts.TextToSpeech$Connection$1.onSuccess(TextToSpeech.java:2131)
        at android.speech.tts.ITextToSpeechCallback$Stub.onTransact(ITextToSpeechCallback.java:202)
        at android.os.Binder.execTransactInternal(Binder.java:1021)
        at android.os.Binder.execTransact(Binder.java:994)

I even tried to work around it using observable as below, but failed with same error:

var detectedFaces: Int by Delegates.observable(-1) { property, oldValue, newValue ->
    tts!!.say("Detected Faces are: $detectedFaces")
}

class MainActivity : AppCompatActivity(), TtsSpeaker.Listener { 
   override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
      scope.launch {
             detectedFaces = FaceDetection.detectFaces(bitmap)
      }
   }
}

Also I tried with changing Dispatchers but failed as well:

scope.launch(Dispatchers.Unconfined) { }

I tried also with callbacks and failed:

var detectedFaces: Int by Delegates.observable(-1) { property, oldValue, newValue ->
    tts!!.say("Detected Faces are: $detectedFaces")
}

class MainActivity : AppCompatActivity(), TtsSpeaker.Listener {
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
         countFaces(bitmap) { faces -> detectedFaces = faces }
    }
}

private fun countFaces(bitmap: Bitmap?, callback: (Int) -> Unit) {
   scope.launch(Dispatchers.Unconfined) {
        val detectedFaces = FaceDetection.detectFaces(bitmap)
        callback(detectedFaces)
   }
}

I also tried async/await as below, and failed as well:

class MainActivity : AppCompatActivity(), TtsSpeaker.Listener {
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
         val faces = countFaces(bitmap)
         Toast.makeText(
              this@MainActivity, "Detected Faces = $faces",
              Toast.LENGTH_SHORT
         ).show()
         tts!!.say("Detected Faces are: $faces")
    }
}

private fun countFaces(bitmap: Bitmap?): Int {
   var faces: Int = 0
   runBlocking {
      val detect = async { FaceDetection.detectFaces(bitmap) }
       runBlocking{
            faces = detect.await()
       }
    }
    return faces
}

In all the trials above, I get the result, and the app speak it, but it crashes after speaking, upon the execution of onTtsSpoken as mentioned in the beginning.

I got it…

The problem was not in the block that is calling the tts!!.speak,the problem was in the onTtsSpoken itself, it was not complete with ToDo() blocks which caused the error, I replaced the ToDo() by {} and things are fine now.

    override fun onTtsSpoken() {
        when (state) {
            State.INITIALIZING, State.CONFIRMING_ACTION, State.TIMEOUT -> {
                state = State.LISTENING_TO_KEYPHRASE
                pocketsphinx!!.startListeningToActivationPhrase()
            }
            State.CONFIRMING_KEYPHRASE -> {
                state = State.LISTENING_TO_ACTION
                pocketsphinx!!.startListeningToAction()
            }
            State.LISTENING_TO_KEYPHRASE -> {} // replaced the TODO() by {}
            State.LISTENING_TO_ACTION -> {} // replaced the TODO() by {}
            null -> {} // replaced the TODO() by {}
        }
    }