Handle audio output for audio glasses and display glasses using Text to Speech

Applicable XR devices
This guidance helps you build experiences for these types of XR devices.
Audio &
Display Glasses

One way to communicate with users is by using Text to Speech (TTS). Built-in Android TTS requires no additional libraries and works offline. These characteristics make TTS well suited for handling error conditions on audio glasses or when the display is off. You can use TTS features through the TextToSpeech class.

Instantiate TextToSpeech

TextToSpeech initializes asynchronously. Instantiate it in your projected activity's onCreate method, wait for TextToSpeech.SUCCESS in OnInitListener, and configure speech AudioAttributes before speaking. If onStart runs before initialization finishes, queue the greeting and speak it inside the listener instead of dropping the utterance:

private var tts: TextToSpeech? = null
private var isTtsReady = false
private var pendingUtterance: Pair<String, String>? = null

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

    tts = TextToSpeech(this) { status ->
        if (status == TextToSpeech.SUCCESS) {
            tts?.setAudioAttributes(
                AudioAttributes.Builder()
                    .setUsage(AudioAttributes.USAGE_MEDIA)
                    .setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
                    .build()
            )
            isTtsReady = true
            pendingUtterance?.let { (text, utteranceId) ->
                pendingUtterance = null
                speakWhenReady(text, utteranceId)
            }
        }
    }
}

Notify the user when TTS starts

For audio glasses (and display glasses when the display is off), confirm launch immediately in onStart by playing the generic app_open.ogg earcon with SoundPool while waiting for the TTS engine to initialize, and then speak your opening message:

override fun onStart() {
    super.onStart()

    // Preloaded with SoundPool; see the Gemini Live guide for a helper example.
    earconPlayer.playAppOpen()
    speakWhenReady("Welcome to Android XR glasses.", "welcome_utterance")
}

private fun speakWhenReady(text: String, utteranceId: String) {
    if (!isTtsReady) {
        pendingUtterance = text to utteranceId
        return
    }
    tts?.speak(text, TextToSpeech.QUEUE_FLUSH, null, utteranceId)
}

Key points about the code

  • Tracking isTtsReady and pendingUtterance avoids dropping an utterance if onStart runs before the TextToSpeech engine finishes initializing.
  • TextToSpeech.QUEUE_FLUSH specifies that the text should be spoken immediately and any earlier utterance in the queue should be interrupted.
  • The utteranceId (in this case "welcome_utterance") identifies when TTS finishes speaking that utterance. For details, see UtteranceProgressListener.

Interrupt TTS

If your app needs to interrupt TTS—for example, before Gemini Live starts playing model audio so two voices don't overlap—call the stop method:

// Interrupts the current utterance and discards queued utterances.
tts?.stop()

Clean up TTS resources

Clean up resources when your activity is destroyed by calling shutdown inside your activity's onDestroy method:

override fun onDestroy() {
    super.onDestroy()

    tts?.shutdown()
}