Handle audio output for AI glasses using Text to Speech

Applicable XR devices
This guidance helps you build experiences for these types of XR devices.
AI Glasses

One of the ways that you can communicate with your users is by using Text to Speech (TTS) technology. TTS is built into Android (requiring no additional libraries) and works even when offline. These characteristics makes TTS ideal for handling error conditions in displayless mode. You can reference TTS features using the TextToSpeech class.

Instantiate TextToSpeech

We recommend instantiating the TextToSpeech class on your AI glasses activity's onCreate() method so that it's available for the lifetime of the Activity:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    tts = TextToSpeech(this) { status ->
        if(status == TextToSpeech.SUCCESS) {
        // Initialization successful
        }else {
            // Initialization failed
        }
    }
    ...
}

Notify the user when TTS starts

For displayless (audio-only) experiences, let the user know that your app successfully launched by notifying them in the onStart() method:

override fun onStart() {
  super.onStart()

  tts?.speak("Welcome to Android XR Glasses!",
  TextToSpeech.QUEUE_FLUSH,
  null,
  "welcome_utterance")
  ...
}

Key points about the code

  • TextToSpeech.QUEUE_FLUSH indicates that the text should be spoken immediately and any other TTS utterance should be interrupted.
  • The utteranceId, in this case "welcome_utterance", is used to identify when this text is finished being spoken. For more information, see the UtteranceProgressListener.

Interrupt TTS

If your app ever needs to interrupt TTS, call the stop() method:

// This interrupts the current utterance and discards other utterances in the queue.
tts?.stop()
...

Clean up TTS resources

You should clean up resources when your activity is destroyed by calling the shutdown() method within the activity's onDestroy() method:

override fun onDestroy() {
    super.onDestroy()

    tts?.shutdown()
}