Code:

package com.yotamarker.lg200221

import android.speech.tts.TextToSpeech
import android.util.Log
import java.util.*

class TTSV2(tts: TextToSpeech?) : TextToSpeech.OnInitListener{
    //see Mouth class for documentation
    private val tts:TextToSpeech? = tts
    override fun onInit(status:Int) {
        if (status == TextToSpeech.SUCCESS&&tts!=null) {
            // set US English as language for tts
            val result = tts!!.setLanguage(Locale.US)

            if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                Log.e("TTS","The Language specified is not supported!")
            } else {
                //buttonSpeak!!.isEnabled = true
            }

        } else {
            Log.e("TTS", "Initilization Failed!")
        }
    }
}


2nd class you need to add :

Code:

package com.yotamarker.lg200221

import android.speech.tts.TextToSpeech
class Mouth(tts:TextToSpeech) {
    /* setup :
    1 add this class and TTSV2.kt class to your project
    2 add this 3 code lines to MianActivity :
    private var tts: TextToSpeech? = null;private var mouth: Mouth? = null //tts setup line 1 of 3 global var declaration
    tts = TextToSpeech(this, TTSV2(tts));mouth= Mouth(tts!!)//tts setup line 2 of 3 place in onCreate methode
    mouth!!.onDestroy()//tts setup line 3 of 3 place in onDestroy() of MainActivity
    3 use example : mouth!!.speakOut("hadouken")
    * */
    private val tts:TextToSpeech
    init{
        this.tts = tts
    }
    fun speakOut(sentence:String) {
        tts.speak(sentence, TextToSpeech.QUEUE_FLUSH, null, "")
    }
    fun onDestroy() {
        if (tts != null) {
            tts!!.stop()
            tts!!.shutdown()
        }
    }
}


see class doc for how to use in the main activity :chobit: