0

I have been trying to find a way to have my application speak a response instead of just provide a text response. I feel like it should be easy but I cannot find what I need. I have found many examples of taking user input and converting it to speech, but I only want to have a predetermined phrase spoken. Can anyone tell me what I am missing when searching for info or provide any sources?

Edit: I have been trying at this for a while now. This is the code I have at the moment. There are no errors and the rest of the app works fine, but the message is not spoken on launch.

class MainActivity : AppCompatActivity() {

//declaring variable for speech recognition
private val REQ_CODE_SPEECH_INPUT = 100
private lateinit var mTTS:TextToSpeech


override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    mTTS = TextToSpeech(applicationContext, TextToSpeech.OnInitListener { status ->
        if (status != TextToSpeech.ERROR){
            //if there is no error then set language
            mTTS.language = Locale.getDefault()
        }
    })

    val helloWorld: String = "hello there"

    mTTS.speak(helloWorld, TextToSpeech.QUEUE_FLUSH, null)
    }
}
Markus Kauppinen
  • 3,025
  • 4
  • 20
  • 30
  • Could link to examples of converting user input to speech? Sounds like something close to what you are looking for. – David Soroko Mar 23 '20 at 20:49
  • Any text-to-speech library will do. Not many of them work offline though and have a good quality. Rhvoice probably. – Nikolay Shmyrev Mar 23 '20 at 21:20
  • The `TextToSpeech` object takes time to initialize. There's a callback function (the "TextToSpeech.OnInitListener" in your code) run when it's ready. If you call `speak()` before that (as you do) it fails. – Markus Kauppinen Mar 24 '20 at 12:57

0 Answers0