<br />


Applicable XR devices This guidance helps you build experiences for these types of XR devices. [Learn about XR device types →](https://developer.android.com/develop/xr/devices) ![](https://developer.android.com/static/images/develop/xr/ai-glasses-icon.svg) Audio \&  
Display Glasses [](https://developer.android.com/develop/xr/devices#audio-display) [Learn about XR device types →](https://developer.android.com/develop/xr/devices)

<br />

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`](https://developer.android.com/reference/kotlin/android/speech/tts/TextToSpeech) class.
[![](https://developer.android.com/static/images/picto-icons/machine-learning.svg) Enhance with AI Try out the ML Kit GenAI Text-to-Speech API, which uses a generative AI model to produce speech audio that is more natural sounding than standard TTS.](https://developers.google.com/ml-kit/genai/text-to-speech/android)

### Instantiate TextToSpeech

We recommend instantiating the `TextToSpeech` class on your [projected
activity's](https://developer.android.com/develop/xr/jetpack-xr-sdk/glasses/support-different-types#activity-lifecycle) [`onCreate`](https://developer.android.com/reference/kotlin/android/app/Activity#onCreate(android.os.Bundle)) method so that it's available for the lifetime
of the [`Activity`](https://developer.android.com/reference/kotlin/android/app/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`](https://developer.android.com/reference/kotlin/android/app/Activity#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`](https://developer.android.com/reference/kotlin/android/speech/tts/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`](https://developer.android.com/reference/kotlin/android/speech/tts/UtteranceProgressListener).

## Interrupt TTS

If your app ever needs to interrupt TTS, call the [`stop`](https://developer.android.com/reference/kotlin/android/speech/tts/TextToSpeech#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`](https://developer.android.com/reference/kotlin/android/speech/tts/TextToSpeech#shutdown) method within the activity's [`onDestroy`](https://developer.android.com/reference/kotlin/android/app/Activity#onDestroy()) method:

    override fun onDestroy() {
        super.onDestroy()

        tts?.shutdown()
    }