In my Android app, I created TTS (Text To Speech) function as below:
package hasan.tts_mobile
import android.content.Context
import android.speech.tts.TextToSpeech
import android.speech.tts.UtteranceProgressListener
import android.util.Log
import java.util.Locale
class TtsSpeaker(context: Context, private val listener: Listener) : TextToSpeech.OnInitListener {
private var isInitialized = false
private var ttsEngine: TextToSpeech? = null
interface Listener {
fun onTtsInitialized()
fun onTtsSpoken()
}
init {
ttsEngine = TextToSpeech(context, this)
}
override fun onInit(status: Int) {
if (status == TextToSpeech.SUCCESS) {
ttsEngine!!.language = Locale.US
ttsEngine!!.setOnUtteranceProgressListener(object : UtteranceProgressListener() {
override fun onStart(utteranceId: String) {
Log.i(TAG, "onStart")
}
override fun onDone(utteranceId: String) {
Log.i(TAG, "onDone")
listener.onTtsSpoken()
}
override fun onError(utteranceId: String, errorCode: Int) {
Log.w(TAG, "onError ($utteranceId). Error code: $errorCode")
}
override fun onError(utteranceId: String) {
Log.w(TAG, "onError")
}
})
ttsEngine!!.setPitch(1f)
ttsEngine!!.setSpeechRate(1f)
isInitialized = true
Log.i(TAG, "TTS initialized successfully")
listener.onTtsInitialized()
} else {
Log.w(TAG, "Could not open TTS Engine (onInit status=$status). Ignoring text to speech")
ttsEngine = null
}
}
fun say(message: String) {
if (!isInitialized || ttsEngine == null) {
Log.w(TAG, "TTS is not initialized yet, be patient")
return
}
ttsEngine!!.speak(message, TextToSpeech.QUEUE_ADD, null, UTTERANCE_ID)
}
fun onDestroy() {
if (ttsEngine != null) {
ttsEngine!!.stop()
ttsEngine!!.shutdown()
}
}
companion object {
private val TAG = TtsSpeaker::class.java.simpleName
private const val UTTERANCE_ID = BuildConfig.APPLICATION_ID + ".UTTERANCE_ID"
}
}
Trying to call it as:
class MainActivity : AppCompatActivity(), TtsSpeaker.Listener {
var tts: TtsSpeaker? = null
private var state: State? = null
private enum class State {
INITIALIZING,
LISTENING_TO_KEYPHRASE,
CONFIRMING_KEYPHRASE,
LISTENING_TO_ACTION,
CONFIRMING_ACTION,
TIMEOUT
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
if (!isPermissionGranted(permission.RECORD_AUDIO)) {
requestAudioPermission(this)
} else {
Toast.makeText(
this@MainActivity, "Audio permission is granted",
Toast.LENGTH_SHORT
).show()
tts = TtsSpeaker(this, this)
}
}
override fun onSpeechRecognizerReady() {
state = State.INITIALIZING
tts!!.say("I'm ready!")
}
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 -> TODO()
State.LISTENING_TO_ACTION -> TODO()
null -> TODO()
}
}
override fun onDestroy() {
super.onDestroy()
tts!!.onDestroy()
}
}
But it fail, and not speaking anything!